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

前端 未结 13 2236
梦毁少年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:20

    function checkMultiArrayValue($array) {
            global $test;
            foreach ($array as $key => $item) {
    
                if(!empty($item) && is_array($item)) {
                    checkMultiArrayValue($item);
                }else {
                    if($item)
                     $test[$key] = $item;
    
                }
            }
            return $test;   
        }
    
     $multiArray = array(    
                    0 =>  array(  
                          "country"   => "",  
                          "price"    => 4,  
                          "discount-price" => 0,  
                   ),);
    
    $test = checkMultiArrayValue($multiArray);
    echo "
    "
    print_r($test);
    

    Will return array who have index and value

提交回复
热议问题