Last active
August 27, 2018 09:09
-
-
Save technoknol/bc057772ee7e6ebace78 to your computer and use it in GitHub Desktop.
search key=>value pair in Multidimensional Array in PHP
This file contains hidden or 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
<?php | |
// Author: Shyam Makwana | |
// Website : http://shyammakwana.me | |
$a = array( | |
2 => array('name' => 'john', 'age' => 34), | |
3 => array('name' => 'doe', 'age' => 45), | |
'4c' => array('family' => array('toe' => array('name' => 'doe', 'age' => 39) | |
, 'age' => 45)) | |
); | |
function in_array_r($needle, $val, $arr) { | |
$flag = false; | |
if (isset($arr[$needle]) && $arr[$needle] == $val) { | |
$flag = true; | |
} else { | |
foreach ($arr as $k => $subArr) { | |
if (is_array($subArr)) { | |
if (in_array_r($needle, $val, $subArr)) { | |
$flag = true; | |
break; | |
} | |
} | |
} | |
} | |
return $flag; | |
} | |
echo '<pre>'; | |
var_dump(in_array_r('age', 39, $a)); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment