如何在foreach循环下合并一个数组中的数组

时间:2017-04-07 07:21:57

标签: php arrays sorting multidimensional-array filter

我有一个array我希望在foreach循环下合并,请提供解决方案我该怎么做。

Array
(
    [0] => Array
        (
            [id] => 377556
            [name] => 8 Ball Pool iOS App - US *
    )
)

Array
(
    [0] => Array
        (
            [id] => 377555
            [name] => test data
    )
)

我想要下面的结果所以请提供解决方案如何合并一个数组中的数据。

Array
(
    [0] => Array
        (
            [id] => 377556
            [name] => 8 Ball Pool iOS App - US *
    )
    [1] => Array
        (
            [id] => 377555
            [name] => test data
    )
)

4 个答案:

答案 0 :(得分:2)

PHP code demo

guides(fill = guide_legend(override.aes = list(shape = 21)),
       shape = guide_legend(override.aes = list(fill = "black")))

<强>输出:

> Sys.setlocale(locale = "es")
[1] ""
Warning message:
In Sys.setlocale(locale = "es") :
OS reports request to set locale to "es" cannot be honored

答案 1 :(得分:1)

如果你在谈论像这样的数组

SELECT  
CONCAT(Firstname, ' ' ,Secondname) AS 'Name',
Allcatch.Rekord, Allcatch.Spiecies
From Members
INNER JOIN Catch ON Catch.MemberID = Members.MemberID
INNER JOIN
(SELECT 
max(Weight) AS 'Rekord', Spiecies
FROM Catch 
GROUP BY Spiecies) AS Allcatch ON Allcatch.Rekord = Catch.Weight AND  Allcatch.Spiecies = Catch.Spiecies

你可以像

一样编码
Array
(
[0] => Array
    (
        [0] => Array
            (
                [id] => 1
                [name] => test name1
            )

    )

[1] => Array
    (
        [0] => Array
            (
                [id] => 2
                [name] => test name2
            )

    )

[2] => Array
    (
        [0] => Array
            (
                [id] => 3
                [name] => test name3
            )

    )

)

给你

$result=[];
foreach($main_array as $array){
    $result = array_merge($result, $array);
}
print_r($result);

希望这会有所帮助

答案 2 :(得分:0)

  

@Er Nilay Parekh只需使用foreach(),如下所示:

<?php
$Array1 =   array(
                array(
                    array(
                            "id" => 377556,
                            "name" => "8 Ball Pool iOS App - US *"
                    )
                ),
                array(
                     array(
                            "id" => 377555,
                            "name" => "test data"
                    )
                )
            );
foreach($Array1 as $key => $value){
    foreach ($value as $key1 => $value1) {
        $Array1[$key] = $value1;
    }
}

echo "<pre>";
print_r($Array1);

答案 3 :(得分:0)

由于您的索引是数字,只需使用array_merge($array1, $array2)即可。这假设没有重复的id,如果有的话,你必须做额外的工作。