Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.