duanji1026
2017-04-07 07:21
浏览 276
已采纳

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

I have an array i want to merge under foreach loop please provide solution how can i do it.

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

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

I want below result so please provide solution how can i merge data in one array .

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

图片转代码服务由CSDN问答提供 功能建议

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

  Array 
(
 [0] => Array 
(
 [id] => 377556 \  n [name] => 8 Ball Pool iOS App  -  US * 
)
)
 
Array 
(
 [0] =>数组
(
 [id] => 377555 \  n [name] =>测试数据
)
)
   
 
 

我想要下面的结果所以请提供解决方案我该怎么办? 合并一个数组中的数据。

  Array 
(
 [0] =>数组
(
 [id] => 377556 
 [name]  => 8球池iOS应用程序 - 美国* 
)
 [1] =>数组
(
 [id] => 377555 
 [名称] =>测试数据
)
  )
   
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • dpdhf02040 2017-04-07 07:24
    已采纳

    PHP code demo

    <?php
    $array1=Array
    (
        0 => Array
            (
                "id" => 377556,
                "name" => "8 Ball Pool iOS App - US *"
        )
    );
    
    $array2=Array
    (
        0 => Array
            (
                "id" => 377555,
                "name" => "test data"
        )
    );
    $newArray=array($array1,$array2);
    $result=array();
    foreach($newArray as $value)
    {
        $result=array_merge($result,$value);
    }
    print_r($result);
    

    Output:

    Array
    (
        [0] => Array
            (
                [id] => 377556
                [name] => 8 Ball Pool iOS App - US *
            )
    
        [1] => Array
            (
                [id] => 377555
                [name] => test data
            )
    
    )
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • douchui1488 2017-04-07 07:35

    @Er Nilay Parekh simply do it with foreach() like below:

    <?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);
    
    评论
    解决 无用
    打赏 举报
  • douyin4561 2017-04-07 07:40

    if you are talking about array like these

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

    you can code Like

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

    gives you

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

    Hope this will help

    评论
    解决 无用
    打赏 举报
  • dsh1102 2017-04-07 07:40

    As your index is number, just use array_merge($array1, $array2) is ok. This suppose there is no duplicate ids, you have to do extra work if it has.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题