Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

$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']);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading