php-在数组值前面加上键

php-在数组值前面加上键,php,arrays,Php,Arrays,我有一个数组。看起来像这样 $choices = array( array('label' => 'test1','value' => 'test1'), array('label' => 'test2','value' => 'test2'), array('label' => 'test3','value' => 'test3'), ) 现在我想在$choices数组中预加这个值 array('label' => 'All','value' =&g

我有一个数组。看起来像这样

$choices = array(
array('label' => 'test1','value' => 'test1'),
array('label' => 'test2','value' => 'test2'),
array('label' => 'test3','value' => 'test3'),
)
现在我想在
$choices
数组中预加这个值

array('label' => 'All','value' => 'all'),
看起来我无法使用
array\u unshift
函数,因为我的数组有键


有人能告诉我如何预编吗?

您的
$choices
数组只有数字键,因此
array\u unshift()
将完全满足您的需要

$choices = array(
    array('label' => 'test1','value' => 'test1'),
    array('label' => 'test2','value' => 'test2'),
    array('label' => 'test3','value' => 'test3'),
);
echo $choices[0]['label']; // echoes 'test1'

$array_to_add = array('label' => 'All','value' => 'all');
array_unshift($choices, $array_to_add);

/* resulting array would look like this:
$choices = array(
    array('label' => 'All','value' => 'all')
    array('label' => 'test1','value' => 'test1'),
    array('label' => 'test2','value' => 'test2'),
    array('label' => 'test3','value' => 'test3'),
);
*/
echo $choices[0]['label']; // echoes 'All'

是的,很好用。即使在$choices中没有明确定义的键,每个都必须有键。我不明白为什么不能使用
array\u unshift
;每个数组都有键。看起来你可以。外部数组是一个带有数字键的列表。你在里面放了什么(一个独特的关联数组)并不重要。是的,它可以工作。谢谢7分钟后我会接受你的回答