php更改数组键值

时间:2015-10-21 17:11:07

标签: php arrays

原始数组采用以下格式:

        [att] => Array
            (
                [0] => Array
                    (
                        [id] => 18
                        [day_id] => 27
                    )

                [1] => Array
                    (
                        [id] => 20
                        [day_id] => 32
                    )

            )

和数组键需要按day_id更改,如下所示:

        [att] => Array
            (
                [27] => Array
                    (
                        [id] => 18
                        [day_id] => 27
                    )

                [32] => Array
                    (
                        [id] => 20
                        [day_id] => 32
                    )

            ) 

1 个答案:

答案 0 :(得分:2)

最简单的方法:

$array['att'] = array_column($array['att'], null, 'day_id');