1

I have used the following code to get array and merge them.
On the print_r(arr1) I get following array. In $arr I am trying to merge array but when i print_($arr) at the end of for-each I get same array.
Am I doing wrong array merge? How can i combine or merge it?

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

Array which i get on 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
        )

)
3
  • what you want in $arr after finish foreach Commented Apr 2, 2013 at 10:41
  • @thumbernirmal:suppose i have above three array in Foreach and want that in single array. Commented Apr 2, 2013 at 10:43
  • Edited in this question please check that. Commented Apr 2, 2013 at 10:53

2 Answers 2

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

This $arr variable was never initialized, so nothing plus $arr1 equals to $arr1.

Also why is this code inside a foreach?

Sign up to request clarification or add additional context in comments.

3 Comments

:code inside foreach because each time i get new array and want to merge that in single array.
but each time you get a different $row4 value, not a different $arr1!
:i have not show whole code but by calling showreply function i get above array.
1

try this...i think this is what you are looking for

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

          $arr[]=$row4; 
    }

  print_r($arr)

3 Comments

:where i get merge result.
check i edited...not working then...print out the array you want after for each loop

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.