PHP array_merge() 函数不能保留数字键


PHP array_merge() function can not keep the numeric keys

输入

数组中带有数字键的值将使用结果数组中从零开始的递增键重新编号。上面就是PHP手册说的,我要保留原来的数字键,怎么办?

$arr_from_color_group = Model_Edit_Colorgroup::find('all', array('select' => array('display_name')));$temp = array();
     foreach ($arr_from_color_group as $item){
        $oldtemp = $temp;
         $temp = array($item->id => $item->display_name);
         $temp = array_merge($oldtemp,$temp);
     }
    $form->add(
            'item_color_group_id', 'a forgien key',
            array('options' => $temp, 'type' => 'select', 'value' => 'true')
    )->add_rule('required');

您可以使用 + 运算符合并数组以保留原始键,而不是 array_merge()

$new_arr = $arr1 + $arr2;

查看演示