简体   繁体   English

PHP合并2个具有不同数量元素的数组,一个元素包含$ keys,另一个元素包含$ values

[英]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.

相关问题 如何在PHP中合并4个不同的键和值数组? - How to merge 4 different keys and values arrays in PHP? 如何在PHP中合并来自两个不同数组的键 - How to merge the keys from two different arrays in PHP PHP将具有通用键和不同键的数组数组中的元素合并到最终数组中 - php merge elements in an array of arrays with common and different keys into a final array 使用相同的键将不同数组中的值合并为一个 - Merge values from different arrays to one with the same key 如何将两个数组的第二级数值与不同数量的键合并? - How to merge values of the second level of two arrays with different number of keys? 如何从PHP 2个数组中选择不同的键和值并更改相同的值? - how to pick different keys and values from 2 arrays in php and alter the same? 合并2个PHP数组,使用1个数组中的键和另一个数组中的值,但如果必须删除一个键中不存在的键,则必须删除 - Merge 2 PHP array using the keys from 1 array and values from another with the exception that if the key does not exist in one if has to be removed 用php中的另一个数组值替换一个数组键 - Replace one arrays keys with another arrays values in php 用另一个数组值覆盖数组中的键 - Overwrite keys from array by another arrays values PHP组合来自不同数组的键 - PHP Combine keys from different arrays
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM