Php magento数组\u合并
这个数组合并似乎对我不起作用。我正在尝试将所有数组合并到一个数组中,如下所示:Php magento数组\u合并,php,array-merge,Php,Array Merge,这个数组合并似乎对我不起作用。我正在尝试将所有数组合并到一个数组中,如下所示: foreach ($collection as $result) { $i++; if(empty($json)){ $json = $result->getData(); }else{ $json = array_merge($json,
foreach ($collection as $result) {
$i++;
if(empty($json)){
$json = $result->getData();
}else{
$json = array_merge($json, $result->getData());
}
}
print_r($json);
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
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 ...
}
我在集合中有3个数组。但当我打印r($json);它只显示最后一个这样的数组
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
)
我希望这将合并所有三个数组并打印出来
我有点期待这样:
foreach ($collection as $result) {
$i++;
if(empty($json)){
$json = $result->getData();
}else{
$json = array_merge($json, $result->getData());
}
}
print_r($json);
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
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 ...
}
)
意味着所有数组都应该合并到一个数组中
编辑的
我让它工作。事实上,这就是我想要的:
$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);
谢谢@cillosis,你的例子真的很有帮助。根据php.net上的函数:
如果输入数组具有相同的字符串键,则该键的后一个值将覆盖前一个值。但是,如果数组包含数字键,则后面的值将不会覆盖原始值,而是追加
这意味着,每次您尝试合并它们时,因为它们使用的是同一个键,所以会被覆盖。这就是为什么您只能看到最后一个数组
[编辑]
那么,如何在一个数组中连接具有相同键的多个数组呢 我看不出两个元素如何共享同一个键,除非该键包含另一个类似这样的数组:
foreach ($collection as $result) {
$i++;
if(empty($json)){
$json = $result->getData();
}else{
$json = array_merge($json, $result->getData());
}
}
print_r($json);
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
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 ...
}
这是未经测试的,只是很快就完成了。它应该输出如下内容:
Array(
"po_id": Array(
"first_po_id",
"second_po_id",
"third_po_id"
),
"title": Array(
"first_title",
"second_title",
"third_title"
)
)
当然还有更多的数据。根据php.net上的函数:
如果输入数组具有相同的字符串键,则该键的后一个值将覆盖前一个值。但是,如果数组包含数字键,则后面的值将不会覆盖原始值,而是追加
这意味着,每次您尝试合并它们时,因为它们使用的是同一个键,所以会被覆盖。这就是为什么您只能看到最后一个数组
[编辑]
那么,如何在一个数组中连接具有相同键的多个数组呢 我看不出两个元素如何共享同一个键,除非该键包含另一个类似这样的数组:
foreach ($collection as $result) {
$i++;
if(empty($json)){
$json = $result->getData();
}else{
$json = array_merge($json, $result->getData());
}
}
print_r($json);
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
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 ...
}
这是未经测试的,只是很快就完成了。它应该输出如下内容:
Array(
"po_id": Array(
"first_po_id",
"second_po_id",
"third_po_id"
),
"title": Array(
"first_title",
"second_title",
"third_title"
)
)
当然还有更多的数据。不幸的是,您希望的结构在PHP中是不可能的。读一读;如果尝试将值指定给预先存在的键,它将覆盖任何现有值:
$array = array('foo' => 'bar');
$array = array_merge($array, array('foo' => 'new'));
var_dump($array['foo']); // new
根据您想要操作结果数据的方式,您可以使用:
或者,您可能希望集合按结果分组:
// make $json a copy of $collection
$json = $collection;
// overwrite $result within $json
foreach ($json as &$result) {
$result = $result->getData();
}
编辑:请参阅。不幸的是,您希望的结构在PHP中不可能实现。读一读;如果尝试将值指定给预先存在的键,它将覆盖任何现有值:
$array = array('foo' => 'bar');
$array = array_merge($array, array('foo' => 'new'));
var_dump($array['foo']); // new
根据您想要操作结果数据的方式,您可以使用:
或者,您可能希望集合按结果分组:
// make $json a copy of $collection
$json = $collection;
// overwrite $result within $json
foreach ($json as &$result) {
$result = $result->getData();
}
编辑:请参见。那么,如何将具有相同键的多个数组连接到一个数组中?如何更新问题,向我们展示您的期望@西洛西斯,你不是刚重新发明了吗?@cbuckley哈哈,我想是的。以前没听说过这个函数…现在我知道了!那么,我如何在一个数组中连接具有相同键的多个数组呢?您如何更新问题,向我们展示您的期望@西洛西斯,你不是刚重新发明了吗?@cbuckley哈哈,我想是的。以前没听说过这个函数…现在我知道了!别担心,我没有投反对票。我只是放弃了正义接受通知的评论。别担心,我没有投反对票。我只是放弃了接受通知的评论。