Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I flatten this array format?

Tags:

arrays

php

I am trying to turn the following array

Array
(
    [0] => Array
        (
            [535fd7566a10d96c2ac04d2e26e210c4] => Jane Apple
        )

    [1] => Array
        (
            [3cdacd430d6a70acbe4caf9712a76824] => Johnny Apple
        )
...
)

into the following format.

    Array
    (
                [535fd7566a10d96c2ac04d2e26e210c4] => Jane Apple,
                [3cdacd430d6a70acbe4caf9712a76824] => Johnny Apple,
...
            )

I have tried a few different functions found through the stack-overflow, and google but no luck.

Anyone have any ideas on how I can achieve my desired result?

Thank you,

like image 832
kray Avatar asked Jan 01 '26 15:01

kray


1 Answers

I believe

array_reduce($array, 'array_merge', [])

should do the trick

essentially you want to reduce an array of values (each an array) to one value (an array) hence array_reduce. and merge takes an item (the array) and merges it with another array (the carry in this case) to produce the array you want.

just wanted to provide a non-loop answer ;o)

like image 144
Jakumi Avatar answered Jan 03 '26 06:01

Jakumi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!