在PHP中重命名数组索引

Ran*_*Guy 1 php arrays

我想爆炸一个日期,但想分别将默认索引0,1,2重命名为年,月,日,我试过,但我无法弄明白.这就是现在正在做的事情.

$explode_date = explode("-", "2012-09-28");
echo $explode_date[0]; //Output is 2012
echo $explode_date[1]; //Output is 09
echo $explode_date[2]; //Output is 28
Run Code Online (Sandbox Code Playgroud)

我想要的是

echo $explode_date['year']; //Output is 2012
echo $explode_date['month']; //Output is 09
echo $explode_date['day']; //Output is 28
Run Code Online (Sandbox Code Playgroud)

谢谢..

dec*_*eze 6

list($date['year'], $date['month'], $date['day']) = explode('-', '2012-09-28');
Run Code Online (Sandbox Code Playgroud)

http://php.net/list


com*_*857 6

使用array_combine:

$keys = array('year', 'month', 'day');
$values = explode("-", "2012-09-28");
$dates = array_combine($keys, $values);
Run Code Online (Sandbox Code Playgroud)