如何在这个索引数组前面添加一个键?

时间:2014-09-16 01:51:37

标签: php arrays associative-array

我有这个php索引数组。

array(
        (int) 0 => array(
            'id' => '4',
            'good_time' => '2014-05-01 03:36:00',
        ),
        (int) 1 => array(
            'id' => '5',
            'good_time' => '2014-06-27 11:14:00',
        ),
        (int) 2 => array(
            'id' => '6',
            'good_time' => '2014-06-27 11:20:24'
    )

我想在前面添加一个键,这样当我在json中打印它时,它看起来更有意义。

'Time' => array(
    (int) 0 => array(
        'id' => '4',
        'good_time' => '2014-05-01 03:36:00',
    ),
    (int) 1 => array(
        'id' => '5',
        'good_time' => '2014-06-27 11:14:00',
    ),
    (int) 2 => array(
        'id' => '6',
        'good_time' => '2014-06-27 11:20:24'
    )
)

如何在php中完成?

2 个答案:

答案 0 :(得分:4)

这很容易:

$newArray['Time'] = $otherArray;

$newArray = array('Time' => $otherArray); // now you can add other keys to $newArray

答案 1 :(得分:0)

假设您的数组存储在$a变量中。你可以做到

$a = array('Time' => $a);