0

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.

D.B.
  • 89
  • 1
  • 9

1 Answers1

1

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']);
}
Quirinus
  • 442
  • 1
  • 5
  • 9
  • I can't because the array is dynamic, and id is changed every time when the array is generated. So ,next time array key which starts with id- will be different. – D.B. Aug 06 '22 at 13:17
  • I've updated my answer, this should cover dynamic ids. – Quirinus Aug 06 '22 at 13:28
  • I get the following when I print_r the resulting array: Array ( [id-Pa223] => ) – D.B. Aug 06 '22 at 13:29
  • Thanks this works, even I still have one more array on the top that I don't need it, but I guess I will now will be able to find my way from here. Thanks. – D.B. Aug 06 '22 at 13:38
  • I guess you needed it flattened, I've edited my post with that. – Quirinus Aug 06 '22 at 13:41