[英]PHP merge 2 arrays with different number of elements, $keys from one, $values from another
I want to merge 2 arrays together that have a different number of elements, using the keys from one and the values from another where/if the keys match. 我想使用一个键和另一个键匹配的值将两个具有不同元素数量的数组合并在一起。 The array that contains the desired values may have less elements in it although I would like to retain the resulting empty keys from the original array.
包含所需值的数组中可能包含较少的元素,尽管我想保留原始数组中得到的空键。 For example:
例如:
//Array that contains keys I would like to retain //包含要保留的键的数组
$arr1 = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
//Array that contains values I'd like to use //包含我要使用的值的数组
$arr2 = array(01=>123, 03=>123, 05=>123, 07=>123, 09=>123, 11=>123);
//The desired result with some valueless elements //带有一些无价值元素的期望结果
$results = array(01=>123, 02, 03=>123, 04, 05=>123, 06, 07=>123, 08, 09=>123, 10, 11=>123, 12);
As you can see the results array retains 12 elements but only applies values to where the keys from the 2 arrays match. 如您所见,results数组保留12个元素,但仅将值应用于两个数组中的键匹配的位置。
I have tried $results = array_intersect_key($arr1 + $arr2, $arr2);
我已经尝试过
$results = array_intersect_key($arr1 + $arr2, $arr2);
among other PHP functions as well as: 其他PHP函数以及:
for ($i=1; $i < count($arr1); $i++) {
if (isset($arr2[$i])) {
$arr3[] = $arr2[$i];
} else {
$arr3[] = $arr1[$i];
}
}
print_r($arr3);
No luck as yet. 还没有运气。
Thanks in advance! 提前致谢!
For arrays like this 对于这样的数组
$arr1 = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
$arr2 = array(1=>123, 3=>123, 5=>123, 7=>123, 9=>123, 11=>123);
this should work. 这应该工作。
foreach ($arr1 as $key) {
$results[$key] = isset($arr2[$key]) ? $arr2[$key] : null;
}
or using some other PHP functions: 或使用其他一些PHP函数:
$results = array_replace(array_fill_keys($arr1, null), $arr2);
//Array that contains keys I would like to retain
$arr1 = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
//Array that contains values I'd like to use
$arr2 = array(1=>123, 3=>123, 5=>123, 7=>123, 9=>123, 11=>123);
$result = array_fill_keys(
$arr1,
null
);
array_walk(
$result,
function(&$value, $key) use($arr2) {
$value = (isset($arr2[$key])) ? $arr2[$key] : null;
}
);
var_dump($result);
First set your $arr1
's values to false
to retain just the keys: $arr1 = array_fill_keys(array_keys($arr1), false);
首先将
$arr1
的值设置为false
以仅保留键: $arr1 = array_fill_keys(array_keys($arr1), false);
Or if you're generating $arr1
yourself, define it as such to start with: $arr1 = Array(false, false, false, false /* etc. */);
或者,如果您自己生成
$arr1
,则将其定义为以以下内容开头: $arr1 = Array(false, false, false, false /* etc. */);
Now use the array union operator: 现在使用数组联合运算符:
$result = $arr2 + $arr1;
The + operator returns the right-hand array appended to the left-hand array;
+运算符返回添加到左侧数组的右侧数组; for keys that exist in both arrays — from the docs: http://php.net/manual/en/language.operators.array.php )
用于两个数组中都存在的键-来自文档: http : //php.net/manual/zh/language.operators.array.php )
Then if required sort the array by key: ksort($array);
然后,如果需要,按键对数组排序:
ksort($array);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.