0

配列があります。こんな感じ

$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配列にキーがあるため、関数 を使用できないようです。

誰かが前置する方法を教えてもらえますか?

4

1 に答える 1

2

$choices配列には数字キーしかないので、あなた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'
于 2013-03-07T18:47:29.773 に答える