2016-09-21 61 views
0

我有兩個PHP數組,並且想要將第一個數組的值附加到第二個數組以創建一個新數組,該數組重複原始順序但組合了這兩個值。我已經嘗試了PHP array_merge,但這只是附加新數組,但不能合併爲單個值。將多個數組值添加到單個值中

Array 
(
    [0] => Array 
     (
      [title] => Item 1 
     ) 
    [1] => Array 
     (
      [title] => Item 2 
     ) 
    [2] => Array 
     (
      [title] => Item 3 
     ) 
    [3] => Array 
     (
      [title] => Item 4 
     ) 
) 

第二陣列;

Array 
(
    [0] => Array 
     (
      [count] => 3 
     ) 
    [1] => Array 
     (
      [count] => 6 
     ) 
    [2] => Array 
     (
      [count] => 9 
     ) 
    [3] => Array 
     (
      [count] => 2 
     ) 
) 

Completed array;

Array 
(
    [0] => Array 
     (
      [title_count] => Item 1 (3) 
     ) 
    [1] => Array 
     (
      [title_count] => Item 2 (6) 
     ) 
    [2] => Array 
     (
      [title_count] => Item 3 (9) 
     ) 
    [3] => Array 
     (
      [title_count] => Item 4 (2) 
     ) 
) 
+0

我不認爲有什麼內置的,這將做到這一點。你必須寫一個循環來完成它。我從來沒有見過需要像這樣連接數組鍵。 – Barmar

回答

1

只需使用foreach循環。

$result = array(); 
foreach ($array1 as $i => $element) { 
    $title = $element['title']; 
    $count = $array2[$i]['count']; 
    $result[] = array('title_count' => "$title ($count)"); 
}