views:

164

answers:

5

Hi everyone,

I am trying to create a new array from two current arrays. Tried array_merge, but it will not give me what I want. $array1 is a list of keys that I pass to a function. $array2 holds the results from that function, but doesn't contain any non-available resuls for keys. So, I want to make sure that all requested keys comes out with 'null':ed values, as according to the shown $result array.

It goes a little something like this:

$array1 = array('item1', 'item2', 'item3', 'item4');

$array2 = array(
    'item1' => 'value1',
    'item2' => 'value2',
    'item3' => 'value3'
);

Here's the result I want:

$result = array(
    'item1' => 'value1',
    'item2' => 'value2',
    'item3' => 'value3',
    'item4' => ''  
);

It can be done this way, but I don't think that it's a good solution - I really don't like to take the easy way out and suppress PHP errors by adding @:s in the code. This sample would obviously throw errors since 'item4' is not in $array2, based on the example.

foreach ($keys as $k => $v){
    @$array[$v] = $items[$v]; 
}

So, what's the fastest (performance-wise) way to accomplish the same result?

A: 

Have you looked at array_combine? Here's a link: PHP reference

It seems to do exactly what you want, with one caveat: If the two arrays aren't of equal size, it does nothing. If it is guaranteed that your array of keys will always be longer than your array of values, you could just pad the values array with null values until both arrays are of equal size before calling array_combine.

Endemic
+1  A: 

Instead of throwing errors check that the key exits using array_key_exists

<?php
 foreach($array1 as $key) {
    if (array_key_exists($key, $array2)) {
        $result[$key] = $array2[$key];
    } else {
        $result[$key] = null;
    }
 }
Alistair
+3  A: 

array_fill_keys will build you a nice array you can use in array_merge:

array_merge(array_fill_keys($array1, ''), $array2);

Or, instead of array_merge, you can use the + op which performs a union:

$array2 + array_fill_keys($array1, '');

This one works with numerical keys or mixed numerical/strings :)

Chris Smith
Oh wow I like that! Awesome answer
Alistair
That doesn't work so well if $array1 contains numeric keys...
Industrial
@Industrial: It works for your example! ;) Does the array contain only numeric keys or a mixture?
Chris Smith
Hi! Thanks for your help, man! The array contains both numbers and characters
Industrial
@Industrial - Updated the answer. `+` seems to work fine with a mixed array.
Chris Smith
Great stuff! That was more clever than I ever would have come up with! Thanks a lot for your help, Chris!
Industrial
A: 

In this case you can use array_merge and array_fill_keys:

$result = array_merge(
    array_array_fill_keys($array1,''),
    $array2
);

You should be aware that with array_merge the value in the latter array overwrite the first ones only if the keys are not numbers and, therefore, this method will not work if $array1 contains numerical values. For example with the following values in input, this code will not work properly:

$array1 = array(1,2,3);
$array2 = array("hi"=>"world",2=>"test","other"=>"empty");

Will produce the following:

array(6) {
  [0]=>
  string(0) ""
  [1]=>
  string(0) ""
  [2]=>
  string(0) ""
  ["hi"]=>
  string(5) "world"
  [3]=>
  string(4) "test"
  ["other"]=>
  string(5) "empty"

}

If you are sure that the array keys are all literals you can use it without worries

Eineki
Hi! It's the literal parts that worries me. What happens when not?
Industrial