1

I am using below code to find an array inside parent array but it is not working that is retuning empty even though the specified key exits in the parent array

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

foreach($cards_parent as $key => $card)
{
    if ($key === 'Cards')
    {
        $cards[] = $cards_parent[$key];
        break;
    }
}

Do you know any array function that will search parent array for specified key and if found it will create an array starting from that key?

2
  • It's hard to tell from your example what the issue is. Give a sample of what $cards_parent might look like. If you mean the key is at an unknown depth within the array, look into recursive iteration. Commented Jun 4, 2010 at 6:49
  • 1
    Why is $cards an array? It seems that it will hold only one value anyway... Commented Jun 4, 2010 at 6:54

3 Answers 3

1

you want array_key_exists()

takes in a needle (string), then haystack (array) and returns true or false.

in one of the comments, there is a recursive solution that looks like it might be more like what you want. https://www.php.net/manual/en/function.array-key-exists.php#94601

Sign up to request clarification or add additional context in comments.

Comments

1

here you can use recursion:

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 Comments

Uuuuh, $_GLOBAL... >_< You should really return instead.
No. I shouldn`t. I need to pass all array. If return - recursion will be interrupted. $_Global is here to rememder finded values and to go to the end of array.
It is perfectly possible to return and merge values throughout recursive calls. All your recursive invocations return implicitly anyway. At least use a static variable if you have to.
0

Could you please put a print_r($feedData) up? I ran the below code

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

And it returned a populated array:

Array ( [0] => Array ( [0] => hellow [1] => jwalk ) )

So your code is correct, it may be that your array $feedData is not.

Comments

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.