如何防止array_merge重新编号数字键

i have an array which looks like this:

Array ( 
    [0] => Array ( [unit_id] => 1 [unit_name] => Clown Fish) 
    [1] => Array ( [unit_id] => L [unit_name] => Liter ) 
    [2] => Array ( [unit_id] => 2 [unit_name] => Elephant  ) 
    [3] => Array ( [unit_id] => 3 [unit_name] => Water Bottle ) 
    [4] => Array ( [unit_id] => 4 [unit_name] => Office Seating ) 
    [5] => Array ( [unit_id] => 5 [unit_name] => Green Green Grass ) 
)

then, i wrote a function

function array_to_list($arr_data, $str_key, $str_value) {
    $arr_list = array();
    if (is_array($arr_data)) {
        foreach($arr_data as $arr_value) {
            if (isset($arr_value[$str_key]) && isset($arr_value[$str_value])) {
                $arr_list = array_merge($arr_list, array($arr_value[$str_key] => $arr_value[$str_value]));
            }
        }
    }
    return $arr_list;
}

to convert the array to look like this

Array ( 
    [1] => Clown Fish ) 
    [L] => Liter ) 
    [2] => Elephant ) 
    [3] => Water Bottle ) 
    [4] => Office Seating ) 
    [5] => Green Green Grass ) 
)

but the output, instead, is

Array ( 
    [0] => Clown Fish ) 
    [L] => Liter ) 
    [1] => Elephant ) 
    [2] => Water Bottle ) 
    [3] => Office Seating ) 
    [4] => Green Green Grass ) 
)

i assume this has something to do with the nature of array_merge itself, which according to php manual "Values in the input array with numeric keys will be renumbered with incrementing keys starting from zero in the result array."

is there a way to so i can produce the intended result with or without array_merge?

dongtao9095
dongtao9095 谢谢你这么快回答。看到解决方案如此简单但我在谷歌前一段时间无法找到它是令人尴尬的。谢谢。
大约 9 年之前 回复
dongpangfu6322
dongpangfu6322 programmingfacts.com/...
大约 9 年之前 回复

3个回答

Why don't you just try a much simpler approach:

function array_to_list($arr_data, $str_key, $str_value) {
    $arr_list = array();
    if (is_array($arr_data)) {
        foreach($arr_data as $arr_value) {
            if (isset($arr_value[$str_key]) && isset($arr_value[$str_value])) {

                // This is the changed line:
                $arr_list[ $arr_value[$str_key] ] = $arr_value[$str_value];

            }
        }
    }
    return $arr_list;
}

This just sets the values on the output array directly. There's no reason to make a new array and merge it each time. It also should be a bit faster.

dongzheng8463
dongzheng8463 我想知道为什么你的解决方案早些时候没有出现。 谢谢。 更快,我认为我可以节省开销而不是旧的。
大约 9 年之前 回复

use '+' operator instead of array_merge():

function array_to_list($arr_data, $str_key, $str_value) {
$arr_list = array();
if (is_array($arr_data)) {
    foreach($arr_data as $arr_value) {
        if (isset($arr_value[$str_key]) && isset($arr_value[$str_value])) {
            $arr_list = $arr_list + array($arr_value[$str_key] => $arr_value[$str_value]);
        }
    }
}
return $arr_list;

}

dongshuxi3105
dongshuxi3105 +1因为简单和准确
大约 8 年之前 回复
dougan6402
dougan6402 这正是我所需要的(为了更简单的目的)...使用数字键将“0”键添加到数组中而不重新编号键。
8 年多之前 回复

drop the array_merge() and append the key and value to new $arr_list

function array_to_list($arr_data, $str_key, $str_value) {
    $arr_list = array();
    if (is_array($arr_data)) {
        foreach($arr_data as $arr_value) {
            if (isset($arr_value[$str_key]) && isset($arr_value[$str_value])) {
                $arr_list[$arr_value[$str_key]] = $arr_value[$str_value];
            }
        }
    }
    return $arr_list;
}
dongpeixiong5943
dongpeixiong5943 我实际上都在使用这两个:D。 对我来说是一种运动。 谢谢。
大约 9 年之前 回复
doumengjing1500
doumengjing1500 或者像@PlagueEditor说:$ arr_list = $ arr_list +(array($ arr_value [$ str_key] => $ arr_value [$ str_value]));
大约 9 年之前 回复
立即提问