Check if a specific value exists at a specific key in any subarray of a multidimensional array

前端 未结 13 2242
梦毁少年i
梦毁少年i 2020-11-27 18:42

I need to search a multidimensional array for a specific value in any of the indexed subarrays.

In other words, I need to check a single column of the multidimension

13条回答
  •  忘掉有多难
    2020-11-27 19:23

    If you have to make a lot of "id" lookups and it should be really fast you should use a second array containing all the "ids" as keys:

    $lookup_array=array();
    
    foreach($my_array as $arr){
        $lookup_array[$arr['id']]=1;
    }
    

    Now you can check for an existing id very fast, for example:

    echo (isset($lookup_array[152]))?'yes':'no';
    

提交回复
热议问题