Heim >Backend-Entwicklung >PHP-Tutorial >php【数组对比输出】解决办法
php【数组对比输出】
<br />$array1=array("1","2","3","4","5","6");//此数据值固定。<br />$array2=array("1","3","6","4");//此数据值不固定。<br />
<br />$arr = array();<br />$x;<br />foreach($array2 as $k => $v) {<br /> x = array_search($v,$array1);<br /> if (x) {<br /> $arr[$k] = $v;<br /> } else {<br /> $arr[$k] = 0;<br /> }<br />}<br />
$array1=array("1","2","3","4","5","6");//此数据值固定。<br />$array2=array("1","3","6","4");//此数据值不固定。<br />$arr = array();<br />foreach($array1 as $v){<br /> $arr[] = in_array($v, $array2) ? $v : 0;<br />}<br />print_r($arr);
$a = array("1","2","3","4","5","6");<br />$b = array("1","3","6","4");<br />$c = $a;<br />foreach(array_diff($a, $b) as $k=>$v) $c[$k] = 0;<br /><br />print_r($c);
Array<br />(<br /> [0] => 1<br /> [1] => 0<br /> [2] => 3<br /> [3] => 4<br /> [4] => 0<br /> [5] => 6<br />)<br /><br />
<br />$array1=array("1","2","3","4","5","6");//此数据值固定。<br />$array2=array("1","3","6","4");//此数据值不固定。<br />
$c = array_diff($array1, $array2);
foreach($c as $k=>$v){
$array1[$k] = 0;
}
print_r($array1);