1

This array_merge doesn't seem to work for me. I am trying merge all the arrays in one array like this:

foreach ($collection as $result) {
          $i++;
          if(empty($json)){
            $json = $result->getData();                                
          }else{
            $json = array_merge($json, $result->getData());                
          }
      }
      print_r($json);

I have 3 arrays in the collection. But when I do print_r($json); it only shows me the last array like this.

Array ( 
    [po_id]           => 3 
    [title]           => Test3 
    [geo_address]     => M1 2FF 
    [latitude]        => 53.449137 
    [longitude]       => -2.364551 
    [display_address] => testing 
    [url]             => http://testing.com 
    [phone]           => 0321654987 
    [status]          => 1 
    [type]            => 1 
    [created_time]    => 2012-01-26 11:07:05 
    [update_time]     => 2012-01-26 11:10:13 
    [distance]        => 3708.40724665926 
)

I am expecting this to merge all three arrays and print that out.

I'm kinda expecting it like this:

Array ( 
[po_id]           => 1 
[title]           => Test1 
[geo_address]     => M1 2FF
[po_id]           => 2 
[title]           => Test2 
[geo_address]     => M2 2FF  
[po_id]           => 3 
[title]           => Test3 
[geo_address]     => M3 2FF 

)

Means all the arrays should be merged in on array.

EDITTED

I have it working. In fact this what I was looking for:

$json = array();

    foreach ($collection as $key=>$result) {

         $data = $result->getData();

         $json[$key]['postorefinder_id'] = $data['postorefinder_id'];
         $json[$key]['title'] = $data['title'];
         $json[$key]['geo_address'] = $data['geo_address'];
         $json[$key]['latitude'] = $data['latitude'];
         $json[$key]['latitude'] = $data['latitude'];
         $json[$key]['longitude'] = $data['longitude'];
         $json[$key]['display_address'] = $data['display_address'];
         $json[$key]['url'] = $data['url'];
         $json[$key]['phone'] = $data['phone'];
         $json[$key]['status'] = $data['status'];
         $json[$key]['type'] = $data['type'];
         $json[$key]['created_time'] = $data['created_time'];
         $json[$key]['update_time'] = $data['update_time'];
         $json[$key]['distance'] = $data['distance'];
    }
    return json_encode($json);

Thanks @cillosis, your example really helped.

1
  • Don't worry, I didn't downvote. I only dropped the comment of righteous acceptance notification. Commented Feb 2, 2012 at 1:12

2 Answers 2

4

According to the array_merge() function on php.net:

If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.

This means, every time you try to merge them, because they are using the same key, it just gets overwritten. That's why you only see the last array.

[EDIT]

So how would I concatenate multiple arrays with the same keys in one array?

I don't see how two elements can share the same key unless that key contained another array like this:

foreach ($collection as $result) {

   // Get collection of data
   $data = $result->getData();

   // Assign each result to multi-dimensional array
   $json['po_id'][] = $data['po_id'];
   $json['title'][] = $data['title'];
   $json['geo_address'][] = $data['geo_address'];
   $json['latitude'][] = $data['latitude'];
   // ... the rest of them go here ...

}

That is untested, just threw it together real quick. It should output something like:

Array(
   "po_id": Array(
      "first_po_id",
      "second_po_id",
      "third_po_id" 
   ),
   "title": Array(
      "first_title",
      "second_title",
      "third_title" 
   )
)

With more data than that of course.

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

3 Comments

So how would I concatenate multiple arrays with the same keys in one array?
How about you update the question, showing us what you expect? @cillosis haven't you just reinvented array_merge_recursive?
@cbuckley Haha, I suppose I did. Haven't heard of that function before...now I know!
1

Unfortunately the structure you're hoping for is not possible in PHP. Have a read about the array type; if you try to assign a value to a pre-existing key, it overwrites any existing value:

$array = array('foo' => 'bar');
$array = array_merge($array, array('foo' => 'new'));
var_dump($array['foo']); // new

Depending on how you want to manipulate the resulting data, you can use array_merge_recursive:

$json = array();

foreach ($collection as $result) {
    $json = array_merge_recursive($json, $result->getData());
}

or alternatively you might want the collections to group by result:

// make $json a copy of $collection
$json = $collection;

// overwrite $result within $json 
foreach ($json as &$result) {
    $result = $result->getData(); 
}

EDIT: See example output for each of these approaches.

Comments

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.