phparraysmultidimensional-array

Replace each whole row of a 2d array with its first element value


I'm banging my head against what I am sure is a simple fix to re-structure an array. I don't have any choice over how the array is given to me:

Array
(
    [author] => Array
        (
            [0] => John Doe
        )

    [journal] => Array
        (
            [0] => Biology
        )
)

But I need the following:

Array
(
    [author] => John Doe
    [journal] => Biology
)

I've been working on various routes, but my brain doesn't work like this right now.


Solution

  • $result = [];
    foreach ($data as $key => $value) {
        $result[$key] = $value[0];
    }
    var_dump($result);