Get subarray of multidimensional array by key in php

I have an array of following structure

Array
(
[id-on56R] => Array
    (
        [doc_num] => 121
        [id-on56R] => Array
            (
                [error_count_low] => 0
                [sum_my_count] => 0
                [data] => Array
                    (
                        [0] => Array
                            (
                                [id] => xx9238-333
                                [total] => 9287
                            )

                        [1] => Array
                            (
                                [id] => tty299-992228
                                [total] => 7441
                            )

                        [2] => Array
                            (
                                [id] => zeyyt-003088
                                [total] => 28741
                            )

                    )

            )

    )

)

I want to extract the subarray data with its keys (id, total) and their values.

[0] => Array
    (
        [id] => xx9238-333
        [total] => 9287
    )

[1] => Array
    (
        [id] => tty299-992228
        [total] => 7441
    )

 [2] => Array
    (
        [id] => zeyyt-003088
        [total] => 28741
    )

I tried solutions in this answer but nothing worked for me.
Then, I tried to flatten the array, using the following code:

$result = array();
array_walk_recursive($buckets,function($v) use (&$result){ $result[] = $v; });

But the above code is removing the keys, which is not what I want.

>Solution :

If I denote the first array as $originalArray, you can access what you want by simply doing this:

$originalArray['id-on56R']['id-on56R']['data']

Unless you have some other requirements that you didn’t share here – otherwise you’ll have to explain and expand on the structure of the $originalArray (maybe a broader example would help).


Edit:

Maybe there are multiple things in that array?

In that case, the simplest solution is to just use a loop:

$resultingArray = [];
foreach ($originalArray as $key => $value)
{
    $resultingArray[$key] = $originalArray[$key][$key]['data'];
}

Then you will have data saved in $resultingArray under the $key name:

$resultingArray = [
    'id-on56R' => [
        0 => [
            'id' => 'xx9238-333',
            'total' => '9287',
        ],
        1 => [
            'id' => 'tty299-992228',
            'total' => '7441',
        ],
        2 => [
            'id' => 'zeyyt-003088',
            'total' => '28741',
        ],
    ],
    'id-some-other' => [
        0 => [
            'id' => '...',
            'total' => '...',
        ],
        1 => [
            'id' => '...',
            'total' => '...',
        ],
        '...',
    ],
    '...',
];

Edit3:

I guess you wanted it flattened, this will work:

$resultingArray = [];
foreach ($originalArray as $key => $value)
{
    $resultingArray = array_merge($resultingArray, $originalArray[$key][$key]['data']);
}

Leave a Reply