PHP:合并2个数组

时间:2016-03-27 13:06:36

标签: php arrays array-merge

我现在已经尝试了一天,并且无法让它发挥作用。

我从分页源获取信息(比如本例中的3页。所以我有3个数组要合并:

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
                )
        )
)

我正在寻找的结果是将它们合并为一个我可以返回的数组。

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
                )
        )
)

我遇到的问题是,由于记录的索引号相同,因此使用array_merge它不会起作用。

我尝试了以下操作,但这也不起作用。

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

3 个答案:

答案 0 :(得分:1)

试试这个:

$array1 = array(
'status' => 'Active',
'nrEntries' => 6,
'entries' => array(
    array(
        'sgname' => 'Merc',
        'timeentered' => '2016-02-08 04:30:00'
    ),
    array(
        'sgname' => 'bystander',
        'timeentered' => '2016-03-17 20:55:00'
    )
  )
);

$array2 = array(
'status' => 'Active',
'nrEntries' => 6,
'entries' => array(
    array(
        'sgname' => 'Elvis',
        'timeentered' => '2016-03-08 04:30:00'
    ),
    array(
        'sgname' => 'marcAR',
        'timeentered' => '2016-03-07 20:55:00'
    )
   )
 );

$result = array_merge_recursive($array1, $array2);
$result['status']    = array_unique($result['status'])[0];
$result['nrEntries'] = array_unique($result['nrEntries'])[0];
echo "<pre>";
print_r($result);
echo "</pre>";

这给出了以下内容:

  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
            )

      )

   )

希望这有帮助。

答案 1 :(得分:1)

array_merge()不是递归的,因此它将整个条目数组替换。有一个名为array_merge_recursive()的函数,但在您的情况下也不会起作用,因为它将在statusnrEntries下创建包含所有数组值的数组。

您需要做的是合并“大”页面数组,然后分别合并条目。这看起来像这样:

// Keep all pages in an array
$pages = [$pageOne, $pageTwo, $pageThree];

// Merge the page arrays
$result = array_merge(...$pages);

// Clear entries as they only contain the data from the last page
$result['entries'] = [];

// Merge entries with the entries of each page separately
foreach ($pages as $page) {
    $result['entries'] = array_merge($result['entries'], $page['entries']);
}

这是我能想到的最简单的例子。我希望它可以帮助您了解正在发生的事情,因此您可以重构它以满足您的需求。

只要您的条目数组具有从0开始的数字键,array_merge将附加值而不是替换它们。

答案 2 :(得分:1)

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