how to prevent array_merge to renumber numeric keys
Asked Answered
R

3

9

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?

Roi answered 22/8, 2011 at 4:26 Comment(2)
programmingfacts.com/…Assess
thanks for answering so fast. it is embarrassing for me to see the solution is so simple yet i can't find it when googling a while ago. thank you.Roi
L
1

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.

Litman answered 22/8, 2011 at 4:42 Comment(1)
i wonder myself why your solution didn't come to my head earlier. thanks. being faster, i think i can save overhead rather than with the old one.Roi
A
17

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;

}

Atticism answered 22/8, 2011 at 4:42 Comment(1)
This was exactly what I needed (for a simpler purpose) ... to prepend a '0' key to an array with numeric keys without renumbering the keys.Damascus
L
1

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.

Litman answered 22/8, 2011 at 4:42 Comment(1)
i wonder myself why your solution didn't come to my head earlier. thanks. being faster, i think i can save overhead rather than with the old one.Roi
T
0

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;
}
Thunell answered 22/8, 2011 at 4:42 Comment(2)
or like @Assess said: $arr_list = $arr_list + (array($arr_value[$str_key] => $arr_value[$str_value]));Thunell
i'm using both actually :D. a sort of exercise for me. thank you.Roi

© 2022 - 2024 — McMap. All rights reserved.