Search code examples
phparraysmultidimensional-arrayarray-merge

Merge column data from multiple multidimensional arrays


I've been trying the following for a day now and can't get it to work.

I'm getting information from a paginated source (say 3 pages in this example. So I've got 3 arrays to merge:

Array
(
    [status] => Active
    [nrEntries] => 6
    [entries] => Array
        (
            [0] => Array
                (
                    [sgname] => Merc
                    [timeentered] => 2016-02-08 04:30:00
                )

            [1] => Array
                (
                    [sgname] => bystander
                    [timeentered] => 2016-03-17 20:55:00
                )
        )
)

Array
(
    [status] => Active
    [nrEntries] => 6
    [entries] => Array
        (
            [0] => Array
                (
                    [sgname] => Elvis
                    [timeentered] => 2016-03-08 04:30:00
                )

            [1] => Array
                (
                    [sgname] => marcAR
                    [timeentered] => 2016-03-07 20:55:00
                )
        )
)

Array
(
    [status] => Active
    [nrEntries] => 6
    [entries] => Array
        (
            [0] => Array
                (
                    [sgname] => Killer
                    [timeentered] => 2016-03-09 05:30:00
                )

            [1] => Array
                (
                    [sgname] => MyName
                    [timeentered] => 2016-03-05 21:45:00
                )
        )
)

The result I am looking for is to merge them into 1 array that I can return as a result.

Array
(
    [status] => Active
    [nrEntries] => 6
    [entries] => Array
        (
            [0] => Array
                (
                    [sgname] => Merc
                    [timeentered] => 2016-02-08 04:30:00
                )

            [1] => Array
                (
                    [sgname] => bystander
                    [timeentered] => 2016-03-17 20:55:00
                )

            [2] => Array
                (
                    [sgname] => Elvis
                    [timeentered] => 2016-03-08 04:30:00
                )

            [3] => Array
                (
                    [sgname] => marcAR
                    [timeentered] => 2016-03-07 20:55:00
                )

            [4] => Array
                (
                    [sgname] => Killer
                    [timeentered] => 2016-03-09 05:30:00
                )

            [5] => Array
                (
                    [sgname] => MyName
                    [timeentered] => 2016-03-05 21:45:00
                )
        )
)

The problem that I'm running into is that with array_merge it won't work because of the identical index numbers of the records.

I tried the following, but that doesn't work either.

<?PHP
// add child array to the end of $result
for ($i = 0 ; $i < 2; $i++) {
    $result['entries'][($page*2) + $i][] = $resultChild['entries'][$i];
}
?>

Solution

  • $a1['entries'] = array_merge_recursive($a1['entries'], $a2['entries'], $a3['entries']);