2017-10-18 103 views
0

我想用相同的索引第二阵列仅第一阵列的非空索引中,只需在这种情况下这意味着代替第一阵列索引0与第二阵列索引0:数组索引与另一阵列相同索引替换在PHP

$all_images => array:2 [ 0 => "00fb319e3af47a5600d39248ad5ea9c2.png" 

         1 => null ] 

$images => array:2 [ 0 => "1508159073atouch-q12-tablet-new-white-color-27-09-2017.png" 

        1 => "1508159073atouch-q12-tablet-new-white-color-30-07-2017.png" ] 

结果:

$New_images => array:2 [ 0 => "00fb319e3af47a5600d39248ad5ea9c2.png" 

         1 => "1508159073atouch-q12-tablet-new-white-color-30-07-2017.png" ] 

回答

0

筛选出空值,并添加(两个数组工会):

$New_images = array_filter($all_images) + $images; 
+0

非常感谢你我对此非常疲惫 –

0

我认为你正在寻找阵列合并。

$a = [1 => 'a', 2 => 'b']; 
$b = [1 => 'z']; 

var_dump($b+$a); 

重读后,我误解了这个问题。您可以使用循环填充空值。

foreach ($a as $k => $v) { 
    if (! $v) { 
     $a[$k] = $b[$k]; 
    } 
} 

var_dump($a); 
+0

我觉得你在你的代码忘了'array_merge'。 – AbraCadaver