How to search by key=>value in a multidimensional array in PHP

前端 未结 15 2049
闹比i
闹比i 2020-11-22 00:13

Is there any fast way to get all subarrays where a key value pair was found in a multidimensional array? I can\'t say how deep the array will be.

Simple example arra

15条回答
  •  鱼传尺愫
    2020-11-22 00:58

    function findKey($tab, $key){
        foreach($tab as $k => $value){ 
            if($k==$key) return $value; 
            if(is_array($value)){ 
                $find = findKey($value, $key);
                if($find) return $find;
            }
        }
        return null;
    }
    

提交回复
热议问题