how to prevent array_merge to renumber numeric keys
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;
}
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.