比较来自PHP

问题描述

我目前正在努力比较两个数组。

一个数组($ allRoepnummerArray)包含所有可用的电话号码。

第二个数组($ occupiedRoepnummers)包含已占用的电话号码。

目前我无法比较它们。

我想在表格中提供可用的电话号码。

$allRoepnummerArray = array(
                                '22-101','22-102','22-103','22-104','22-105','22-106','22-107','22-108','22-109','22-110','22-111','22-112','22-113','22-114','22-115','22-116','22-117','22-118','22-119','22-120','22-121','22-122','22-123','22-124','22-125','22-126','22-127','22-128','22-129','22-130',);

                            $occupiedRoepnummers = array();

                            foreach ($roepnummerResults as $roepnummerKey => $roepnummerValue) {
                                array_push($occupiedRoepnummers,$roepnummerValue['roepnummer']);
                            }

                            foreach($allRoepnummerArray as $allRoepnummer) {
                                foreach($occupiedRoepnummers as $occupiedRoepnummer) {

                                    if ($allRoepnummer != $occupiedRoepnummer) {
                                        echo '<th>'.$allRoepnummer.'</th>';
                                    }

                                }
                            }
                            ?>

解决方法

尝试一下:

foreach($allRoepnummerArray as $allRoepnummer) {
if (!in_array($allRoepnummer,$occupiedRoepnummers)) {
                                    echo '<th>'.$allRoepnummer.'</th>';
                                }}
,

您可以用array_diff()减去数组。您可以这样做:

$availableRoepnummers = array_diff($allRoepnummerArray,$occupiedRoepnummers);

然后您可以制作一个$availableRoepnummers的HTML表。

,

使用如下所示的array_intersect()函数将仅重新运行匹配的值

$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("e"=>"red","f"=>"black","g"=>"purple");
$a3=array("a"=>"red","b"=>"black","h"=>"yellow");

$result= array_intersect($a1,$a2);
print_r(array_diff($a1,$result));

结果:Array ( [b] => green [c] => blue [d] => yellow )