如何比较两个数组并为第二个数组wrt 2nd数组中不存在的索引分配0


How to compare two arrays and assign 0 for the index not present in the second array wrt 2nd array

我有first_array,这是我的second_array。

我想在这里做的是,参考我的第一个数组,我想检查第一个数组的每个值是否存在于第二个数组中,如果不存在,我想在偶数索引中添加0,在奇数索引中添加第一个数组。

关于first_array,值:

[0] => 2510-24 
[17] => 2530ya-8G 
[18] => 2530ya-8G-PoEP 
[19] => 2530yb-24 
[20] => 2530yb-8

不存在于第二阵列中。

现在,我想将所有这些值附加到我的第二个数组中,格式为-

[0] => 0 
[1] => 2510-24 
[2] => 0 
[3] => 2530ya-8G 
[4] => 0 
[5] => 2530ya-8G-PoEP 
[6] => 0 
[7] => 2530yb-24 
[8] => 0 
[9] => 2530yb-8

我该怎么做?请引导我。

您可以使用以下内容,但要小心,如果值不在第二个数组中,并且密钥已经存在于第二个阵列中,它将被覆盖

<?php
function compareArrays($arr1, &$arr2) {
    foreach($arr1 as $k => $v) {
        if (!in_array($v, $arr2)) {
            $arr2[$k] = 0;
        }
    }
}
compareArrays($arr1, $arr2);

演示