'array_diff'函数可用于查找数组中缺少的元素。
<?php function absent($my_list) { $my_array = range(min($my_list), max($my_list)); return array_diff($my_array, $my_list); } echo "Elements missing from first array are "; print_r(absent(array(45, 48, 51, 52, 53, 56))); echo "Elements missing from second array are "; print_r(absent(array(99, 101, 104, 105))); ?>
输出结果
Elements missing from first array are Array ( [1] => 46 [2] => 47 [4] => 49 [5] => 50 [9] => 54 [10] => 55 ) Elements missing from second array are Array ( [1] => 100 [3] => 102 [4] => 103 )
定义了一个名为“不存在”的函数,该函数检查以查看最小数量和最大数量并生成该范围内的数组。然后,该函数使用'array_diff'函数返回该数组与原始数组之间的差,该差给出数组中缺少的元素。