willpower Posted March 27, 2012 Share Posted March 27, 2012 Whats the most efficient way of searching within a multi dimensional array? My Array = Array ( [0] => Array ( [0] => Item 1 [title] => Item 1 [1] => 2012-03-21 [eventDate] => 2012-03-21 [2] => 21 [eventDay] => 21 ) [1] => Array ( [0] => Item 2 [title] => Item 2 [1] => 2012-03-21 [eventDate] => 2012-03-21 [2] => 21 [eventDay] => 21 ) ) String to find = '21' If (MY ARRAY contains STRING TO FIND) {} Clearly in this case there are several '21' (s) so if I only wanted to search the [eventDay] keys...would there be a fast effective and efficient manner? Thoughts and help gratefully received. Will Quote Link to comment https://forums.phpfreaks.com/topic/259842-search-multi-dim-array/ Share on other sites More sharing options...
willpower Posted March 28, 2012 Author Share Posted March 28, 2012 Found this...which works... <?php function in_object($val, $obj){ if($val == ""){ trigger_error("in_object expects parameter 1 must not empty", E_USER_WARNING); return false; } if(!is_object($obj)){ $obj = (object)$obj; } foreach($obj as $key => $value){ if(!is_object($value) && !is_array($value)){ if($value == $val){ return true; } }else{ return in_object($val, $value); } } return false; } ?> Usage : <?php $array = array("a", "b", "c"=>array("x", "y"=>array("p", "q"=>"r"))); if(in_object("r", $arrX)){ echo "r is there "; }else{ echo "Its not there "; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/259842-search-multi-dim-array/#findComment-1331754 Share on other sites More sharing options...
The Letter E Posted March 28, 2012 Share Posted March 28, 2012 Check out the comment thread on this page: http://php.net/manual/en/function.array-search.php Looks like some good options there. Quote Link to comment https://forums.phpfreaks.com/topic/259842-search-multi-dim-array/#findComment-1331799 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.