简体   繁体   中英

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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