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.