Jump to content

[SOLVED] array in array


daveoliveruk

Recommended Posts

<?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
?>

Link to comment
https://forums.phpfreaks.com/topic/87574-solved-array-in-array/#findComment-447909
Share on other sites

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 ;)

Link to comment
https://forums.phpfreaks.com/topic/87574-solved-array-in-array/#findComment-447922
Share on other sites

<?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?

Link to comment
https://forums.phpfreaks.com/topic/87574-solved-array-in-array/#findComment-447951
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.