代码之家  ›  专栏  ›  技术社区  ›  Sarfraz

如何从父数组中查找数组?

  •  1
  • Sarfraz  · 技术社区  · 16 年前

    我使用下面的代码来查找父数组内的数组,但是即使指定的键在父数组中退出,返回空数组也是不起作用的

    $cards_parent = $feedData['BetradarLivescoreData']['Sport']['Category']['Tournament']['Match'];
    $cards = array();
    
    foreach($cards_parent as $key => $card)
    {
        if ($key === 'Cards')
        {
            $cards[] = $cards_parent[$key];
            break;
        }
    }
    

    你知道有什么数组函数可以在父数组中搜索指定的键,如果找到它就会创建一个从该键开始的数组吗?

    3 回复  |  直到 8 年前
        1
  •  1
  •   helloandre    16 年前

    你想要吗 array_key_exists()

    接收针(字符串),然后输入haystack(数组)并返回true或false。

    http://us2.php.net/manual/en/function.array-key-exists.php#94601

        2
  •  1
  •   GOsha    16 年前

    function Recursor($arr)
    {
     if(is_array($arr))
     {
      foreach($arr as $k=>$v)
      {
       if($k == 'Cards')
       {
         $_GLOBAL['cards'][] = $card;
       } else {
         Recursor($arr[$k]);
       }
      }
     }
    }
    
    $cards_parent = $feedData['BetradarLivescoreData']['Sport']['Category']['Tournament']['Match'];
    $_GLOBAL['cards'] = array();
    Recursor($cards_parent);
    
        3
  •  0
  •   halfer Jatin Pandey    8 年前

    请你把 print_r($feedData) 向上?我运行了下面的代码

    <?php
     $feedData = array('BetradarLivescoreData' => array('Sport' => array('Category' => array('Tournament' => array('Match' => array('Cards' => array('hellow','jwalk')))))));
     $cards_parent = $feedData['BetradarLivescoreData']['Sport']['Category']['Tournament']['Match'];
    $cards = array();
    
     foreach($cards_parent as $key => $card)
     {
         if ($key === 'Cards')
         {
             $cards[] = $card;
             break;
         }
     }
     print_r($cards);
    

    它返回一个填充数组:

    数组([0]=>数组([0]=>hellow[1]=>jwalk))

    推荐文章