Created
September 19, 2017 08:50
-
-
Save mkdesignn/b66f0bdfcf36c801579d405f9fe6a253 to your computer and use it in GitHub Desktop.
iterate over all multidimensional array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function in_array_r($needle, $haystack, $strict = false) { | |
// iterate over all items | |
foreach ($haystack as $key => $item) { | |
// check if the item is array, if so then call recursive function | |
if (is_array($item)){ | |
$ret = in_array_r($needle, $item, $strict); | |
if( is_array($ret) ) | |
return $ret; | |
} | |
// check if the current item is satisfying the needle | |
else { | |
$needle_length = count($needle); | |
$equal = 0; | |
foreach( $needle as $n_key => $n_value ) | |
( $n_value == $haystack[$n_key] ) ? $equal++ : $equal; | |
if( $equal >= $needle_length ) | |
return $haystack; | |
} | |
} | |
}; | |
$objects2 = array( | |
array("flight_time" => 1, "number"=>20, "test"=>"1"), | |
array("flight_time" => 2, "number"=>30, "test"=>"35"), | |
array("flight_time" => 13, "number"=>31, "test"=>"39"), | |
array("flight_time" => 4, "number"=>41, "test"=>"45"), | |
array("flight_time" => 10, "number"=>34, "test"=>"55"), | |
); | |
foreach( $arr as $key => $value ) | |
in_array_r(["flight_time"=>10, "number"=>34, "test"=>"55"], $objects2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment