1

I have the below array and trying to get the value of the key named 'Summary' from $array variable.

["Rows"]=>
array(1) {
  ["Row"]=>
  array(1) {
    [0]=>
    array(1) {
      ["Rows"]=>
      array(1) {
        ["Row"]=>
        array(1) {
          [0]=>
          array(1) {
            ["Rows"]=>
            array(1) {
              ["Row"]=>
              array(1) {
                [0]=>
                array(1) {
                  ["Summary"]=>
                  array(1) {
                    ["ColData"]=>
                    array(2) {
                      [0]=>
                      array(1) {
                        ["value"]=>
                        string(19) "Some Value"
                      }
                      [1]=>
                      array(1) {
                        ["value"]=>
                        string(7) "2001.00"
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

I have tried following,

array_map(function ($ar) {return $ar['Summary'];}, $array);

But didn't get success.

What am I doing wrong?

5
  • array_map will not go deeper than first level, that's all Commented Jul 24, 2017 at 7:24
  • @u_mulder Edited the question. Please help. Commented Jul 24, 2017 at 7:33
  • i would suggest using array_walk_recursive, i'd give you a snippet if i had time but check out these answers Commented Jul 24, 2017 at 7:40
  • @scosu array_walk_recursive() will only iterate the "leaf-nodes" of the array, it will not work for this case. Commented Jul 24, 2017 at 7:53
  • @mickmackusa There may be dynamic rows before finding Summary. Commented Jul 25, 2017 at 5:34

2 Answers 2

1

Here are three options:

(Demo of first two methods)

You can use a simple recursion function like this:

Code #1:

function recursive($array){
    if(key($array)!=='Summary'){
        return recursive(current($array));
    }
    return current($array);
} 
var_export(recursive($array));

Or you can literally access the desired subarray without using any function calls. You just need to name every key (assuming the structure is static).

Code #2:

$array=[
    'Rows'=>[
        'Row'=>[
            [
                'Rows'=>[
                    'Row'=>[
                        [
                            'Rows'=>[
                                'Row'=>[
                                    [
                                        'Summary'=>[
                                            'ColData'=>[
                                                ['value'=>'Some Value'],
                                                ['value'=>'2001.00']
                                            ]
                                        ]
                                    ]
                                ]
                            ]
                        ]
                    ]
                ]
            ]
        ]
    ]
];
var_export($array['Rows']['Row'][0]['Rows']['Row'][0]['Rows']['Row'][0]['Summary']);

To leverage assumptions about array structure, you can implement a recursive process that "jumps" levels per iteration:

Code #3:

function recursive_jumper($array){
    if(key($array)!=='Summary'){
        return recursive_jumper($array['Rows']['Row'][0]);
    }
    return current($array);
} 
var_export(recursive_jumper($array));

Output (from any of the above methods):

array (
  'ColData' => 
  array (
    0 => 
    array (
      'value' => 'Some Value',
    ),
    1 => 
    array (
      'value' => '2001.00',
    ),
  ),
)
Sign up to request clarification or add additional context in comments.

Comments

1
function searchArray($needle, $haystack, &$matched = null)
{
    if (is_array($haystack) && count($haystack) > 0) {
        foreach ($haystack as $key => $value) {
            if ((string)$key === (string)$needle) {
                if (is_array($value)) {
                    $matched = $value;
                } else {
                    $matched[] = $value;
                }
            } else {
                if (is_array($value) && count($value) > 0) {
                    self::searchArray($needle, $value, $matched);
                }
            }
        }
    }   
    return true;
} 
searchArray('Summary', $yourArray, $found);
var_dump($found);

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.