0

I have an array data like this

    $array = Array ( 
         [abc] => Array ( ) 
         [def] => Array ( )
         [hij] => Array ( ) 
             [media] => Array ( 
                 [video_info] => Array ( ) 
                        [video_variants] => Array ( ) 
                                [1] => Array ( )
                                [2] => Array ( )
    ) 
) 

My code looks something like this

foreach($response->extended_entities->media as $media)
        {
        stuffs
           foreach ($media->video_info->variants as $video) 
               {
               stuffs
               }
        }

I want to check whether the "video_info Key is available in the array or not

I have tried this function but it doesn't work

function multi_array_key_exists($key, $array) {
    if (array_key_exists($key, $array))
        return true;
    else {
        foreach ($array as $nested) {
            foreach ($nested->media as $multinest) {
        if (is_array($multinest) && multi_array_key_exists($key, $multinest))
                return true;
        }
    }
    }
    return false;
}


 if (multi_array_key_exists('video_info',$response) === false)
    {
        return "failed";
    }

Please help me

Original array - https://pastebin.com/2Qy5cADF

7
  • use array_key_exists() in a recursive function to travel all levels of the nested arrays Commented Jun 11, 2018 at 22:02
  • You are calling array_find_element_by_key but the function is called multi_array_key_exists... Commented Jun 11, 2018 at 22:10
  • @b4tch my bad. actually i tried many functions and that one one of it .lemme edit the question Commented Jun 11, 2018 at 22:12
  • @jibsteroos how can i do that? Commented Jun 11, 2018 at 22:12
  • Being able to copy/paste your array structure would improve your chance of people wanting to try code to help you with the problem. Commented Jun 11, 2018 at 22:13

2 Answers 2

1

Here's my approach at writing a function to check the keys of your array using the Recursive Iterator classes...

function isArrayKeyAnywhere( $array, $searchKey )
{
  foreach( new RecursiveIteratorIterator( new RecursiveArrayIterator( $array ), RecursiveIteratorIterator::SELF_FIRST ) as $iteratorKey => $iteratorValue )
  {
    if( $iteratorKey == $searchKey )
    {
      return true;
    }
  }
  return false;
}

$array = [
  'abc'=>[],
  'def'=>[],
  'hij'=>[
    'media'=>[
      'video_info'=>[
        'video_variants'=>[
          [],
          []
        ]
      ]
    ]
  ]
];

var_dump( isArrayKeyAnywhere( $array, 'video_info' ) ); // true
var_dump( isArrayKeyAnywhere( $array, 'foo_bar' ) ); // false
Sign up to request clarification or add additional context in comments.

1 Comment

I really like the SPL Iterator classes because it helps you to do a lot of the heavy lifting with this type of task. php.net/manual/en/spl.iterators.php
1

try something like this (recursion)

$key = "video_info";
$invoke = findKey($array, $key);

function findKey($array, $key)
{
    foreach ($array as $key0 => $value) {
        if (is_array($value)) {
            if ($key === $key0) {
                echo 'hit: key ' . $key . ' is present in the array';
                return true;
            }
            findKey($value, $key); // recursion
        } elseif ($key === $key0) {
            echo 'hit: key ' . $key . ' is present in the array';
            return true;
        } else {
            return false;
        }
    }
}

A small note: this function is significantly faster than the accepted answer (factor 4x)

4 Comments

Hi, when you recurse into findKey() from within, it prints that it found the key but its return boolean isn't passed back to the parent level, eg if(findKey($value,$key)){return true;}
also I feel the duplication of your $key === $key0 check is redundant and could be the first action performed within the foreach loop, then when the key doesn't match and the value is an array, recurse and return if true. 3v4l.org/mISe1
Also you are spot on about performance, I just benched our two solutions, and your one is faster, but only once you start hitting the thousands of repeat calls.
@scuzzy - fair remarks, admittedly it was just a quick little spike I wrote to hand the OP a possible solution with recursion - thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.