Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何从父数组中查找数组?_Php_Arrays - Fatal编程技术网

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

Php 如何从父数组中查找数组?,php,arrays,Php,Arrays,我使用下面的代码来查找父数组中的数组,但即使指定的键存在于父数组中,也无法重新调整为空 $cards_parent = $feedData['BetradarLivescoreData']['Sport']['Category']['Tournament']['Match']; $cards = array(); foreach($cards_parent as $key => $card) { if ($key === 'Cards') { $cards

我使用下面的代码来查找父数组中的数组,但即使指定的键存在于父数组中,也无法重新调整为空

$cards_parent = $feedData['BetradarLivescoreData']['Sport']['Category']['Tournament']['Match'];
$cards = array();

foreach($cards_parent as $key => $card)
{
    if ($key === 'Cards')
    {
        $cards[] = $cards_parent[$key];
        break;
    }
}
您知道有哪个数组函数会在父数组中搜索指定的键,如果找到,它会创建一个从该键开始的数组吗?

您想要什么

接收一个指针(字符串),然后接收haystack(数组),并返回true或false


在其中一条注释中,有一个递归解决方案看起来更像您想要的

你能把
打印($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);

这里您可以使用递归:

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);

很难从你的例子中看出问题所在。请给出一个
$cards\u parent
的示例。如果您的意思是密钥位于数组中的未知深度,请查看递归迭代。为什么
$cards
是数组?似乎它无论如何只会保存一个值……uuuuuuh,
$\u GLOBAL
..>你真的应该
返回
。否。我不应该。我需要通过所有数组。如果返回,递归将被中断$_Global在这里用于重新命名查找到的值并转到数组的末尾。在整个递归调用中返回和合并值是完全可能的。无论如何,所有递归调用都隐式返回。如果必须,至少使用一个静态变量。