[英]php - prepend an array value with key
我有一个阵列。 看起来像这样
$choices = array(
array('label' => 'test1','value' => 'test1'),
array('label' => 'test2','value' => 'test2'),
array('label' => 'test3','value' => 'test3'),
)
现在我想在$choices
数组中添加这个值
array('label' => 'All','value' => 'all'),
看起来我不能使用array_unshift
函数,因为我的数组有键。
谁能告诉我如何前置?
你的$choices
数组只有数字键,所以array_unshift()
会完全按照你的意愿array_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'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.