0

Having Two arrays need to Merge two arrays with matching their keys and rest should be empty. I have tried array_merge(), array_diff(), array_fill() but nothing helped.

Array one

Array
    (
        [5] => PHM
        [4] => ODM
        [3] => N
        [6] => M9
        [10] => RDM9
    )

array 2

Array
  (
    [0] => 01
    [1] => 02
    [2] => 03
    [3] => 04
    [4] => 05
    [5] => 06
    [6] => 07
    [7] => 08
    [8] => 09
    [9] => 10
    [10] => 11
    [11] => 12
    [12] => 13
   )

Final Output

Array
  (
    [0] => null 
    [1] => null 
    [2] => null 
    [3] => N
    [4] => ODM
    [5] => PHM
    [6] => M9
    [7] => null 
    [8] => null 
    [9] => null 
    [10] => RDM9
    [11] =>  null   
    [12] => null 
   )
4
  • 1
    What was the output when using array_merge ? Commented Jun 14, 2016 at 14:31
  • Yeah, it's no use to tell readers which things you've tried, without telling them why those things didn't work. Commented Jun 14, 2016 at 14:33
  • why would you want null for the other array values? Commented Jun 14, 2016 at 14:33
  • need to display in a table as null, Commented Jun 14, 2016 at 14:35

3 Answers 3

5

Make array with all nulls from the 2nd array and replace by values from the 1st array

$arr2 = array_fill_keys(array_flip($arr2), null);
$new = array_replace($arr2, $arr1);
print_r($new);
Sign up to request clarification or add additional context in comments.

2 Comments

I'm affraid there is one problem, what if we have key '20' in first array but not in second? it will appear in the final array as filled
@ConstantineUA Yes. But we need to ask OP is it correct result or not
1

You can pass array 2 to a foreach loop and use an if/else statment to check if the key of array 2 exists in array 1, if so set the value of element $output[$key] to NULL, else assign the value as $arr1[key]

   foreach ($arr2 as $key=> $value){
       $output[$key]=(!array_key_exists($key, $arr1)) ? NULL : $arr1[$key];
    }
    var_dump($output);

10 Comments

anyone care to explain what was the downvote for? while it might not be the fastest solution, it does fulfil the requirement, no??
It's not my downvote, but think that due to global $arr1;
foreach doesn't change variable scope unlike a function.
@spalsh58 Yes, you are absolutely right. I must have done something stupid when I tested it in a rush to lead me into thinking it won't work without the global declaration. Now with a more clear head, that line is redundant. Thank you so much, I've learned something from this. Appreciated.
Could you please elaborate more your answer adding a little more description about the solution you provide?
|
0

Here are 3 more solutions depending on developer taste. 3 Snippet Demo

Clear all of the second array values, then replace the associative elements from the first array.

var_export(
    array_replace(array_map(fn() => null, $arr2), $arr1)
);

Functionally iterate over the second array's keys and attempt to add the mapped values from the first array to the result array or fallback to null.

var_export(
    array_reduce(
        array_keys($arr2),
        fn($result, $k) => $result + [$k => $arr1[$k] ?? null],
        []
    )
);

Use a language construct to iterate over the second array's keys and attempt to add the mapped values from the first array to the result array or fallback to null.

$result = [];
foreach ($arr2 as $k => $_) {
    $result[$k] = $arr1[$k] ?? null;    
}
var_export($result);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.