Search a multi-dimensional array for certain values

前端 未结 2 959
野趣味
野趣味 2021-01-16 23:59

I have a multi-dimensional array in the following format:

[0] = (
    \'id\' => \'1\',
    \'type\' => \'fish\', 
    \'owner\' => \'bob\',
)

[1] =         


        
相关标签:
2条回答
  • 2021-01-17 00:15

    you must iterate array like:

    foreach ($array as $i => $values) {
        print "$i {\n";
        foreach ($values as $key => $value) {
            print "    $key => $value\n";
        }
        print "}\n";
    }
    

    and then check for 'type' key value.... then record which is matched must copy it to new array...

    0 讨论(0)
  • 2021-01-17 00:24

    Loop through the array:

    function loopAndFind($array, $index, $search){
             $returnArray = array();
             foreach($array as $k=>$v){
                   if($v[$index] == $search){   
                        $returnArray[] = $v;
                   }
             }
             return $returnArray;
    }
    
    //use it:
    $newArray = loopAndFind($oldArray, 'type', 'cat');
    
    0 讨论(0)
提交回复
热议问题