【问题标题】:magento array_mergemagento array_merge
【发布时间】:2012-02-01 18:22:01
【问题描述】:

这个 array_merge 似乎对我不起作用。我正在尝试将所有数组合并到一个数组中,如下所示:

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

我在集合中有 3 个数组。但是当我做 print_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 
)

我希望这会合并所有三个数组并将其打印出来。

我有点期待它是这样的:

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 

)

表示所有数组都应该合并到一个数组中。

已编辑

我有它的工作。事实上,这就是我想要的:

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

谢谢@cilosis,你的例子真的很有帮助。

【问题讨论】:

  • 别担心,我没有投反对票。我只删除了正义接受通知的评论。

标签: php array-merge


【解决方案1】:

根据php.net上的array_merge()函数:

如果输入数组具有相同的字符串键,则该键的后一个值将覆盖前一个值。但是,如果数组包含数字键,则后面的值不会覆盖原始值,而是会被追加。

这意味着,每次您尝试合并它们时,因为它们使用相同的密钥,它只会被覆盖。这就是为什么你只能看到最后一个数组。

[编辑]

那么如何将具有相同键的多个数组连接到一个数组中呢?

我看不出两个元素如何共享同一个键,除非该键包含另一个像这样的数组:

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

当然还有更多的数据。

【讨论】:

  • 那么如何将具有相同键的多个数组连接到一个数组中?
  • 如何更新问题,向我们展示您的期望? @cilosis 你不是重新发明了array_merge_recursive吗?
  • @cbuckley 哈哈,我想我做到了。以前没听说过这个功能……现在我知道了!
【解决方案2】:

不幸的是,您希望的结构在 PHP 中是不可能的。阅读有关the array type 的信息;如果您尝试将值分配给预先存在的键,它会覆盖任何现有值:

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

根据你想如何操作结果数据,你可以使用array_merge_recursive

$json = array();

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

或者您可能希望集合按结果分组:

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

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

编辑:见example output for each of these approaches

【讨论】:

    猜你喜欢
    • 2011-06-07
    • 2011-10-26
    • 2012-09-06
    • 1970-01-01
    • 1970-01-01
    • 2012-07-23
    • 2018-08-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多