PHP - Find array inside array. Do check -=if array contains other specific array=-

1k Views Asked by At

How we can do check for availability of specific array inside array?

For example we have multidimensional array:

$arr = array(
  array(1,2,3),
  '12',
  true,
  4,
  array(
    'name1' => array(1,2),
    array(
      'some1' => array(99,98,96),
      4
    ),
    array(4,4)
  )
);

And we want do check for array(99,98,96), do exist it inside our $arr?

2

There are 2 best solutions below

0
On BEST ANSWER

Check this code from source array_search

function recursive_array_search($needle,$haystack) {
    foreach($haystack as $key=>$value) {
        $current_key=$key;
        if($needle===$value OR (is_array($value) && recursive_array_search($needle,$value) !== false)) {
            return $current_key;
        }
    }
    return false;
}

I hope this will work

0
On

Try this:

function findRecursive($arr, $find)
{
    foreach ($arr as $match)
    {
        if (is_array($match))
        {
            if ($match == $find)
            {
                return true;
            } else
            {
                if (findRecursive($match, $find))
                {
                    return true;
                }
            }
        }
    }

    return false;
}