2017-10-07 94 views
0
Array(
[0] => Array 
     (
      [0] => 2017-10-05 
      [1] => 24,57 
      [2] => 24,65 
      [3] => 23,86 
     ) 

    [2] => Array 
     (
      [0] => 2017-10-04 
      [1] => 24,38 
      [2] => 24,675 
      [3]=> 24,24 
     ) 
    ) 

我想這個轉換的第一個元素爲在關聯數組,如何讓每個數組元素的一個關鍵

Array(
[2017-10-05] => Array 
     (

      [0] => 24,57 
      [1] => 24,65 
      [2] => 23,86 
     ) 

    [2017-10-04] => Array 
     ( 
      [0] => 24,38 
      [1] => 24,675 
      [2] => 24,24 
     ) 

類似How to make array first value as key for the second value as value in php array這個問題。但我不能在我的情況下使用array_column

回答

5

您可以使用此代碼array_slice:

$newArray = []; 
foreach($array as $a) { 
    $newArray[$a[0]] = array_slice($a, 1); 
} 

DEMO

+0

很好的解決方案,我想用array_walk要做到這一點,但我認爲這更好,更簡單,謝謝 – beginner

+0

很高興聽到這一點。歡迎。 –

+0

順便說一句,這是另一個版本'$ newArray = []; foreach($ array as $ a){ $ first_ele = array_shift($ a); ($ array as $ a){$ newArray [array_shift($ a)] = $ a;} $ newArray [$ first_ele] = $ a;}' ' – beginner

相關問題