2012-02-01 67 views
1

這個array_merge似乎不適合我。我試圖合併所有的陣列在這樣一個陣列:magento 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 

表示所有陣列應該陣列合併英寸

EDITTED

我有工作。其實這就是我在找的東西:

$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,你的例子確實有幫助。

+0

不要擔心,我沒有downvote。我只放棄了正確的接受通知的評論。 – rdlowrey 2012-02-02 01:12:42

回答

4

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

隨着更多的數據當然。

+0

那麼如何將一個數組中的相同鍵的多個數組連接起來呢? – Hum 2012-02-01 23:08:54

+0

您如何更新問題,向我們展示您的期望? @cillosis你不是剛剛重新[array_merge_recursive](http://php.net/manual/en/function.array-merge-recursive.php)? – cmbuckley 2012-02-01 23:29:23

+0

@cbuckley哈哈,我想我是。之前沒有聽說過這個功能......現在我知道了! – 2012-02-02 00:33:55

1

不幸的是,你希望的結構在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