在PHP中查找2个数组之间的缺失值?

时间:2017-09-26 11:51:17

标签: php arrays

我想找到两个数组之间的缺失值......

$in_id  = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
$out_id = array(0, 1, 3, 5, 6, 7, 8, 9, 10, 11);

在第二个变量$ out_id中缺少值2,4,12我想要这些值......

结果:例如

$missing_values = 2,4,12

2 个答案:

答案 0 :(得分:4)

array_diff

怎么样?
<?php
$array1 = array("a" => "green", "red", "blue", "red");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);

print_r($result);
?>

<强>输出

Array
(
    [1] => blue
)

答案 1 :(得分:1)

您可以使用array_diff()来获取数组

<?php

$in_id  = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
$out_id = array(0, 1, 3, 5, 6, 7, 8, 9, 10, 11);
print_r(array_diff($in_id, $out_id))
?>

Array
(
    [2] => 2
    [4] => 4
    [12] => 12
)