2017-08-05 73 views
-3

Heys球員我有以下的數組:分組陣列由指定的鍵值對

Array 
(
    [0] => Array 
     (
      [date] => 2017-03-31 
      [1] => 7.9950 
     ) 

    [1] => Array 
     (
      [date] => 2017-06-30 
      [1] => 8.3425 
     ) 


    [2] => Array 
     (
      [date] => 2017-03-31 
      [2] => 6.8250 
     ) 

    [3] => Array 
     (
      [date] => 2017-06-30 
      [2] => 10.7725 
     ) 


    [4] => Array 
     (
      [date] => 2017-03-31 
      [3] => 6.4950 
     ) 

    [5] => Array 
     (
      [date] => 2017-06-30 
      [3] => 7.3425 
     ) 

) 

我需要所有共享同一日期的值合併成一個陣列,保留原始鍵如果可能的話。他們是我的id然後json_encode建立一個圖表。

Array 
(
    [0] => Array 
     (
      [date] => 2017-03-31 
      [1] => 7.9950 
      [2] => 6.8250 
      [3] => 6.4950 
     ) 

    [1] => Array 
     (
      [date] => 2017-06-30 
      [1] => 8.3425 
      [2] => 10.7725 
      [3] => 7.3425 
     ) 
) 

有沒有辦法達到這個目的?我查找過類似的問題,在php手冊中查找了array_merge之類的函數,但無法達到期望的結果。任何幫助,將不勝感激。

+2

這一切都始於'foreach' –

+1

你到目前爲止嘗試過什麼? –

回答

0

怎麼是這樣的:

$output = array(); 
foreach($input as $record){ 
    foreach($record as $key => $value){ 
    $output[$record['date']][$key] = $value; 
    } 
} 
//make it a regular array 
$output = array_values($output); 

任何重複的數據將出現一次。