简体   繁体   English

如何在PHP中一次通过多个值搜索多维数组?

[英]How to search a multi-dimensional array by multiple values at once in PHP?

Given the following 2D array:给定以下二维数组:

$data_info_array = array( 
array( 
    'score'   => '100', 
    'name'    => 'Alice', 
    'subject' => 'Data Structures'
), 
array( 
    'score'   => '50', 
    'name'    => 'Bob', 
    'subject' => 'Advanced Algorithms'
), 
array( 
    'score'   => '75', 
    'name'    => 'Charlie', 
    'subject' => 'Distributed Computing'
) 
); 

// this gets the key when I search for the score of 50 from one column
$index = array_search('50', array_column($data_info_array, 'score')); 
echo $index; 

If I want to search by two values I can only think of something like:如果我想按两个值进行搜索,我只能想到以下内容:

 $index1 = array_search('50', array_column($data_info_array, 'score')); 
 $index2 = array_search('Bob', array_column($data_info_array, 'name')); 
 $real_index = ( $index1 === $index2 ) ? $index1 : null; 

Is there a way I can search for score of '50' and name of 'Bob' together and get the index to that only if that combo exists?有没有办法我可以一起搜索“50”的分数和“鲍勃”的名字,并且只有当该组合存在时才能获得索引? Is there a better way do do than what I've come up with?有没有比我想出的更好的方法呢?

You can build your search query as an array and compare the intersection of each item with it.您可以将搜索查询构建为一个数组,并将每个项目与其的交集进行比较。

$search = ['score' => '50', 'name' => 'Bob'];

foreach($data_info_array as $k => $v) {
    if ( $search === array_intersect($v, $search) ) {
        echo $k;
        break;
    }
}

@mickmackusa noticed it is safer to use array_intersect_assoc() here. @mickmackusa 注意到在这里使用array_intersect_assoc()更安全。 He's right because when the multi-dimensional array items are unpredictable, nothing forbids to have items like that:他是对的,因为当多维数组项不可预测时,没有什么禁止这样的项:

['miaou' => '50', 'graou' => 'Bob', 'score' => '50', 'name' => 'Bob']

where the searched values are also present but for other keys.除了其他键之外,搜索值也存在。 In this case array_intersect() returns all correct values (with their corresponding keys of course) regardless the keys in $search , and the comparison with the search array will return false .在这种情况下, array_intersect()返回所有正确的值(当然还有它们对应的键),而不管$search的键是什么,并且与搜索数组的比较将返回false

But using array_intersect_assoc() , you ensure that only values for keys in $search are taken in account.但是使用array_intersect_assoc() ,您可以确保只考虑$search中键的值。

Conclusion: If you let yourself be lulled into sleep by the seeming monotony of multidimensional array items, you won't be immune to surprise when unexpected variations arise.结论:如果你让自己通过多维数组的项目看似单调地哄骗进入睡眠,你将不会被免疫的惊喜时,意想不到的变化所引起的。

You can use array_filter() , which allows you to do as many checks on the contents as you need at the same time...您可以使用array_filter() ,它允许您同时根据需要对内容进行尽可能多的检查...

$output = array_filter($data_info_array, function ($data) {
    return $data['score'] == 50 && $data['name'] == 'Bob';
});

This will give you a list of the matches, so you may need to do [0] (and check if only 1 is returned) if you need a single match.这将为您提供匹配列表,因此如果您需要单个匹配,您可能需要执行[0] (并检查是否仅返回 1)。

Here is a nice one-liner, utilizing PHP arrow functions这是一个很好的单行代码,利用 PHP 箭头函数

$name = "Alice";
$score = "100";

$result = array_filter($data_info_array, fn($data) => $data['name'] == $name && $data['score'] == $score );

print_r($result);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM