Asked  7 Months ago    Answers:  5   Viewed   37 times

I need to get two arrays to merge into one while keeping all the keys in place and listing the values in an array like in this example:

$array1 = array('car' => '3', 'bus' => '2');
$array2 = array('dog' => '1', 'car' => '2', 'bird' => '9');  

$merged = array(
    'car' => array('3','2'), 
    'bus' => array('2',null),
    'dog' => array(null,'1'),
    'bird' => (null,'9')
);

 Answers

67
function merge_common_keys(){
    $arr = func_get_args();
    $num = func_num_args();

    $keys = array();
    $i = 0;
    for ($i=0; $i<$num; ++$i){
        $keys = array_merge($keys, array_keys($arr[$i]));
    }
    $keys = array_unique($keys);

    $merged = array();

    foreach ($keys as $key){
        $merged[$key] = array();
        for($i=0; $i<$num; ++$i){
            $merged[$key][] = isset($arr[$i][$key]) ? $arr[$i][$key] : null;
        }
    }
    return $merged;
}

Usage:

$merged = merge_common_keys($array1,$array2);

PS. It can work with more than two arrays, just pass as many as you want as next arguments.

Wednesday, March 31, 2021
 
LOKESH
answered 7 Months ago
25

Quote from the manual:

Null will be cast to the empty string, i.e. the key null will actually be stored under "".

Additional details about how keys are cast are as follows:

The key can either be an integer or a string. The value can be of any type.

Additionally the following key casts will occur:

  • Strings containing valid decimal integers, unless the number is preceded by a + sign, will be cast to the integer type. E.g. the key "8" will actually be stored under 8. On the other hand "08" will not be cast, as it isn't a valid decimal integer.
  • Floats are also cast to integers, which means that the fractional part will be truncated. E.g. the key 8.7 will actually be stored under 8.
  • Bools are cast to integers, too, i.e. the key true will actually be stored under 1 and the key false under 0.
  • Null will be cast to the empty string, i.e. the key null will actually be stored under "".
  • Arrays and objects can not be used as keys. Doing so will result in a warning: Illegal offset type.

As for this being useful or necessary, this is debatable. You are asked to use integer or string keys and you have been warned about implicit key casting.

Wednesday, March 31, 2021
 
Owen
answered 7 Months ago
52

I think this should do it:

public function iterateKeys(array $array, $joiner, $prepend = NULL) {
    if (!isset($formattedArray)) { $formattedArray = array(); }
    foreach ($array as $key => $value) {
        if(is_array($value)) {
            $formatted_array = array_merge($formattedArray, $this->iterateKeys($value, $joiner, $prepend . $joiner . $key));
        } else {
            $formattedArray[] = $prepend . $joiner . $key;
        }

    }
    return $formattedArray;
}

Since the recursive call returns an array, you need to use array_merge to combine it with what you currently have. And for the non-array case, you need to push the new string onto the array, not replace the array with a string.

Wednesday, March 31, 2021
 
edsk
answered 7 Months ago
52

It's not tested and the easiest solution:

$name_merged_original_order = array();
foreach($name as $key){
    $name_merged_original_order[$key] = array();
    if(array_key_exists($key, $v1)){
        $name_merged_original_order[$key]['v1'] = $v1[$key];
    }
    else{
        $name_merged_original_order[$key]['v1'] = 'N/A';
    }
    if(array_key_exists($key, $v2)){
        $name_merged_original_order[$key]['v2'] = $v2[$key];
    }
    else{
        $name_merged_original_order[$key]['v2'] = 'N/A';
    }
}

sort($name);
$name_merged_asc = array();
foreach($name as $key){
    $name_merged_asc[$key] = array();
    if(array_key_exists($key, $v1)){
        $name_merged_asc[$key]['v1'] = $v1[$key];
    }
    else{
        $name_merged_asc[$key]['v1'] = 'N/A';
    }
    if(array_key_exists($key, $v2)){
        $name_merged_asc[$key]['v2'] = $v2[$key];
    }
    else{
        $name_merged_asc[$key]['v2'] = 'N/A';
    }
}
Saturday, May 29, 2021
 
PHLAK
answered 5 Months ago
46

Try with array_merge_recursive

$A = array('a' => 1, 'b' => 2, 'c' => 3);
$B = array('c' => 4, 'd'=> 5);
$c = array_merge_recursive($A,$B);

echo "<pre>";
print_r($c);
echo "</pre>";

will return

Array
(
    [a] => 1
    [b] => 2
    [c] => Array
        (
            [0] => 3
            [1] => 4
        )

    [d] => 5
)
Wednesday, June 2, 2021
 
ajreal
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :