在关联数组中,如何使每个数组元素的第一个元素成为键

时间:2017-10-07 14:24:56

标签: php arrays

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

1 个答案:

答案 0 :(得分:5)

您可以将此代码用于array_slice:

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

DEMO