开发者

Merging two arrays in PHP

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 no开发者_如何学Ct 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?


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 :)


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;
    }
 }


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


You can use array_merge() to merge one or more arrays.


In the cases where that won't work, such you're not using array_fill_keys or a similar function that returns returns an array type, you can cast to array.

$array1 = (array)$array2 + (array)$array1;


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜