daveoliveruk Posted January 24, 2008 Share Posted January 24, 2008 How do I find out if an array has another (nested) array within it? ANy help would be appreciated! Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted January 24, 2008 Share Posted January 24, 2008 <?php $myArray = array( 0 =>1, 1 => 2, 3 => array(1,2,3,4), 4 => 4); print_r($myArray); ?> Quote Link to comment Share on other sites More sharing options...
rhodesa Posted January 24, 2008 Share Posted January 24, 2008 <?php function hasChildArray ( $array ) { if(is_array($array)){ foreach($array as $child){ if(is_array($child)) return true; } } return false; } $test1 = array( 'abc','def'); $test2 = array('test',$test1); if(hasChildArray($test1)) print "test1 has a child array"; if(hasChildArray($test2)) print "test2 has a child array"; //Should print the following: //test2 has a child array ?> Quote Link to comment Share on other sites More sharing options...
jorgep Posted January 24, 2008 Share Posted January 24, 2008 I guess that you are trying to do this: $arr1 = array( 0 => 'Pedro', 1 => array(20,19,18), 2 =? 'Other_stuff' ) $arr2 = array(20,19,18); So $arr1 contains $arr2 if it is a fixed size, or simple arrays like this, I guess that you could do: echo in_array($arr2,$arr1)?"arr1 contains arr2":"arr1 doesn't contain arr2"; Try that, it might be possible. If it doesn't work, check this page http://us2.php.net/manual/en/function.array.php. In any of the functions at your left there should be the function that you are looking for Quote Link to comment Share on other sites More sharing options...
daveoliveruk Posted January 24, 2008 Author Share Posted January 24, 2008 <?php function hasChildArray ( $array ) { if(is_array($array)){ foreach($array as $child){ if(is_array($child)) return true; } } return false; } $test1 = array( 'abc','def'); $test2 = array('test',$test1); if(hasChildArray($test1)) print "test1 has a child array"; if(hasChildArray($test2)) print "test2 has a child array"; //Should print the following: //test2 has a child array ?> That's great! Thanks for all your help. Another thing, how would I add to this function to see if there is more than one record in the child array? Quote Link to comment Share on other sites More sharing options...
rhodesa Posted January 24, 2008 Share Posted January 24, 2008 function hasChildArray ( $array ) { if(is_array($array)){ foreach($array as $child){ if(is_array($child) && count($child)) return true; } } return false; } Quote Link to comment 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.