dsgsdg206050
2018-06-11 21:58
浏览 71
已采纳

在嵌套的多维数组中查找键值

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

图片转代码服务由CSDN问答提供 功能建议

我有这样的数组数据

  $ array = 数组(
 [abc] =>数组()
 [def] =>数组()
 [hij] =>数组()
 [媒体] =>数组(
 [video_info]  => Array()
 [video_variants] => Array()
 [1] => Array()
 [2] => Array()
)
)
   
 
 

我的代码看起来像这样

  foreach($ response-> extended_entities-> media as $ media)
  {
 stuffs 
 foreach($ media-> video_info->变体为$ video)
 {
 stuffs 
} 
} 
   
 
 

我想检查“video_info密钥是否在数组中可用

我已经尝试过此功能但它不起作用

   function multi_array_key_exists($ key,$ array){  
 if(array_key_exists($ key,$ array))
返回true; 
 else {
 foreach($ array as $ nested){
 foreach($ nested-> media as $ multinest){
  if(is_array($ multinest)&&  multi_array_key_exists($ key,$ multinest))
返回true; 
} 
} 
} 
返回false; 
} 
 
 
 if(multi_array_key_exists('video_info',$ response)==  = false)
 {
返回“失败”; 
} 
   
 
 

请帮帮我

原始阵列 - https://pastebin.com/2Qy5cADF

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douchungu0859 2018-06-11 22:25
    已采纳

    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
    
    点赞 打赏 评论
  • duanlie3187 2018-06-11 22:21

    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)

    点赞 打赏 评论

相关推荐 更多相似问题