重新定义数组键php

时间:2013-01-21 13:22:30

标签: php arrays

我有一个数组。

Array
(
    [0] => Array
        (
            [TotalClicksPerDay] => 5
            [Date] => 2012-11-26
            [weekDay] => 2
        )

    [1] => Array
        (
            [TotalClicksPerDay] => 13
            [Date] => 2012-11-27
            [weekDay] => 6
        )

)

我想通过使用值[weekday]作为键来重新定义键。

因此,在上面的示例中,新数组的读取方式如下:

Array
(
    [2] => Array
        (
            [TotalClicksPerDay] => 5
            [Date] => 2012-11-26
            [weekDay] => 2
        )

    [6] => Array
        (
            [TotalClicksPerDay] => 13
            [Date] => 2012-11-27
            [weekDay] => 6
        )
)

1 个答案:

答案 0 :(得分:2)

foreach ($old_array as $values) {
    $new_array[$values['weekDay']] = $values;
}