合并多个多维数组中的列数据

问题描述 投票:0回答:3

我已经尝试了以下一天但无法让它工作。

我从分页源获取信息(在本例中为 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
                )
        )
)

我正在寻找的结果是将它们合并到 1 个数组中,我可以将其作为结果返回。

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];
}
?>
php arrays multidimensional-array array-merge
3个回答
1
投票
$a1['entries'] = array_merge_recursive($a1['entries'], $a2['entries'], $a3['entries']);

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
投票

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

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

// 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
就会追加值而不是替换它们。

© www.soinside.com 2019 - 2024. All rights reserved.