Search code examples
phparray-merge

magento array_merge


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.


Solution

  • 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.