Php 使用另一个数组中的值查找数组中的值
这是我的第一个数组,其中我有我想要的单词的id数组:Php 使用另一个数组中的值查找数组中的值,php,arrays,Php,Arrays,这是我的第一个数组,其中我有我想要的单词的id数组: $needlearray = (0 => 12421, 1 => 58902, 2 => 912, 3 => 42); 然后,第二个数组包含具有单词数据的所有单词: $haystackarray = ( 0 => array('id' => 42, 'word' => "hello", 'otherdata' => "other"), 1 => array('id' => 1242
$needlearray = (0 => 12421, 1 => 58902, 2 => 912, 3 => 42);
然后,第二个数组包含具有单词数据的所有单词:
$haystackarray = (
0 => array('id' => 42, 'word' => "hello", 'otherdata' => "other"),
1 => array('id' => 12421, 'word' => "good", 'otherdata' => "other"),
2 => array('id' => 58902, 'word' => "hello", 'otherdata' => "other"),
3 => array('id' => 5222, 'word' => "hello", 'otherdata' => "other"),
4 => array('id' => 912, 'word' => "hello", 'otherdata' => "other"),
5 => array('id' => 43290, 'word' => "hello", 'otherdata' => "other"),
6 => array('id' => 2312, 'word' => "hello", 'otherdata' => "other")
);
我希望以尽可能快的方式输出,使用$neederaray
值从$haystackarray
中查找“id”。e、 g.使用上述示例,我希望得到的输出是:
$result = (
0 => array('id' => 12421, 'word' => "good", 'otherdata' => "other"),
1 => array('id' => 58902, 'word' => "hello", 'otherdata' => "other"),
2 => array('id' => 912, 'word' => "hello", 'otherdata' => "other"),
3 => array('id' => 42, 'word' => "hello", 'otherdata' => "other"),
);
注:
- 目前我正在使用带有数组搜索的
循环。它非常慢foreach
- 必须保持
的原始顺序$neederArray
- 实际数据可以包含
中的数千个值,也可以包含$neederaray
中的数千个数组$haystackarray
- 速度非常重要,因为这是大型流程的一部分
- 您确实希望在数据库中执行此操作。您可以这样做:
$list = implode(',', $needlearray);
$query = "SELECT id, word, otherdata FROM table
WHERE id IN($list)
ORDER BY FIELD(id, $list)";
- 获取列表中
所在的行id
- 按列表顺序按
排序id
foreach()快,也可能不比快:
- 根据
id
- 翻转指针以获取作为索引的值
- 查找交点(常用关键点)
顺序将无法维持,因此您需要按针对干草堆进行排序,因此数据库是一种方法。您确实希望在数据库中执行此操作。您可以这样做:
$list = implode(',', $needlearray);
$query = "SELECT id, word, otherdata FROM table
WHERE id IN($list)
ORDER BY FIELD(id, $list)";
- 获取列表中
id
所在的行
- 按列表顺序按
id
排序
如果您需要在PHP中执行此操作,那么它可能比foreach()快,也可能不比快:
- 根据
id
- 翻转指针以获取作为索引的值
- 查找交点(常用关键点)
顺序将无法维持,因此您需要按针对干草堆进行排序,因此数据库是一种方法。您可以使用array\u intersect函数
<?php
$needlearray = [0 => 12421, 1 => 58902, 2 => 912, 3 => 42];
$haystackarray = [
0 => array('id' => 42, 'word' => "hello", 'otherdata' => "other"),
1 => array('id' => 12421, 'word' => "good", 'otherdata' => "other"),
2 => array('id' => 58902, 'word' => "hello", 'otherdata' => "other"),
3 => array('id' => 5222, 'word' => "hello", 'otherdata' => "other"),
4 => array('id' => 912, 'word' => "hello", 'otherdata' => "other"),
5 => array('id' => 43290, 'word' => "hello", 'otherdata' => "other"),
6 => array('id' => 2312, 'word' => "hello", 'otherdata' => "other")
];
$result = array_intersect_key($haystackarray, array_intersect(array_column($haystackarray, 'id'), $needlearray));
echo "<pre>";
print_r($result);exit();
您可以使用array\u intersect函数
<?php
$needlearray = [0 => 12421, 1 => 58902, 2 => 912, 3 => 42];
$haystackarray = [
0 => array('id' => 42, 'word' => "hello", 'otherdata' => "other"),
1 => array('id' => 12421, 'word' => "good", 'otherdata' => "other"),
2 => array('id' => 58902, 'word' => "hello", 'otherdata' => "other"),
3 => array('id' => 5222, 'word' => "hello", 'otherdata' => "other"),
4 => array('id' => 912, 'word' => "hello", 'otherdata' => "other"),
5 => array('id' => 43290, 'word' => "hello", 'otherdata' => "other"),
6 => array('id' => 2312, 'word' => "hello", 'otherdata' => "other")
];
$result = array_intersect_key($haystackarray, array_intersect(array_column($haystackarray, 'id'), $needlearray));
echo "<pre>";
print_r($result);exit();
那么,您希望我们在这里做什么?看起来您正在尝试在内存中管理相当于整个数据库的内容。如果速度是个问题,那你就做错了。如果此数据不在数据库中,则应该是。$filtered=array\u intersect\u key($haystackarray,array\u intersect(array\u column($haystackarray,'id'),$neederaray))
如果必须在PHPI中执行,则希望使用某种php函数来计算$result。类似于array_intersect的东西,我可以将针的值与草堆中的“id”匹配。它来自一个数据库。我如何在查询中设置它?如果您是通过数据库进行的,那么(…)子句中的WHERE id是合适的。那么,您希望我们在这里做什么?看起来您正试图在内存中管理相当于整个数据库的内容。如果速度是个问题,那你就做错了。如果此数据不在数据库中,则应该是。$filtered=array\u intersect\u key($haystackarray,array\u intersect(array\u column($haystackarray,'id'),$neederaray))
如果必须在PHPI中执行,则希望使用某种php函数来计算$result。类似于array_intersect的东西,我可以将针的值与草堆中的“id”匹配。它来自一个数据库。如何在查询中进行设置?如果是通过数据库进行设置,则(…)子句中的WHERE id是适当的。遗憾的是,这没有保留原始$neederArray顺序。遗憾的是,这没有保留原始$neederArray顺序。谢谢,我已经使用orderby字段查询了数据库,它工作得很好:)我刚刚注意到,当我执行查询时,它只接收唯一的值。$query本身是正确的。有没有一种方法可以查询数据库并包含重复项?谢谢,我已经使用ORDER BY字段查询了数据库,它工作得很好:)我刚刚注意到,当我执行查询时,它只接收唯一的值。$query本身是正确的。是否有方法查询数据库并包含重复项?