简体   繁体   English

在多维数组中搜索值

[英]Search multidimensional array for value

I wonder if there is better (faster) way to search for value in multidimensional array than looping through every item.我想知道是否有比遍历每个项目更好(更快)的方法来搜索多维数组中的值。

Lets say i have可以说我有

$id_to_search = '16819976033';

And array which is pretty big和相当大的数组

Array
(
 [0] => Array
    (
        [id] => Array
            (
                [0] => 16771055710
                [1] => 16776555710
                [2] => 16819976033
            )

        [o] => 21566
        [p] => 12597.66
    )

[1] => Array
    (
        [id] => Array
            (
                [0] => 14089762
            )

        [o] => 12606
        [p] => 1747.49
    )
etc ...
)

I can find it if i loop through each item and than compare them but its very slow because array is big.如果我遍历每个项目并比较它们,我可以找到它,但它非常慢,因为数组很大。

You can use by array_search function in PHP:您可以在 PHP 中通过array_search function 使用:

$key = array_search($id_to_search, array_column($YourArray, 'id'));

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

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