合并单个数组中的数组

时间:2013-04-02 10:37:26

标签: php codeigniter array-merge

我使用以下代码来获取数组并合并它们 在print_r(arr1)上我得到以下数组。在$ arr我试图合并数组但是当我在for-each结尾处打印_($ arr)时,我得到相同的数组。
我做错了阵列合并吗? 我该如何组合或合并?

foreach($q1->result_array() as $row4)
 {  
        $arr1 = $q1->result_array();
        echo"<pre>"; 

            print_r($arr1); 

        echo"</pre>";
        $arr = array_merge($arr, $arr1);
        echo "<br/>";
        $id = $row4['id'];
        $parent_id = $row4['parent_id'];
        if(!empty($arr1))
        {
            $this->showreply($id);                          
        }

 }
 print_r($arr); 

我在print_r上获得的数组($ arr1):

Array
(
    [0] => Array
        (
            [id] => 69
            [reply] => First reply to Reply
            [parent_id] => 68
            [postid] => 0
            [us_id] => 41
            [added_by] => Shailesh
            [photo] => 9.jpg
            [added_on] => 2013-04-01 16:06:13
        )

)

Array
(
    [0] => Array
        (
            [id] => 70
            [reply] => Reply to Nested Reply
            [parent_id] => 69
            [postid] => 0
            [us_id] => 41
            [added_by] => Shailesh
            [photo] => 9.jpg
            [added_on] => 2013-04-01 16:07:24
        )

)

Array
(
    [0] => Array
        (
            [id] => 52
            [reply] => Reply on demand
            [parent_id] => 70
            [postid] => 0
            [us_id] => 50
            [added_by] => swapnil
            [photo] => 
            [added_on] => 2013-03-29 16:27:57
        )

)

2 个答案:

答案 0 :(得分:1)

$arr = array_merge($arr, $arr1);

这个$ arr变量从未初始化,所以加上$ arr1等于$ arr1。

为什么这个代码在foreach中呢?

答案 1 :(得分:1)

试试这个...我认为这就是你要找的东西

    foreach($q1->result_array() as $row4)
    {  
         $arr1 = $q1->result_array();

          $arr[]=$row4; 
    }

  print_r($arr)