简体   繁体   English

比较php中两个数组的数据

[英]Compare data from two arrays in php

I am currently struggling to compare two arrays.我目前正在努力比较两个数组。

The first array ($allRoepnummerArray) contains all the callnumbers available.第一个数组 ($allRoepnummerArray) 包含所有可用的电话号码。

The second array ($occupiedRoepnummers) contains the call numbers that are occupied.第二个数组 ($occupiedRoepnummers) 包含被占用的索书号。

At the moment I am unable to compare them.目前我无法比较它们。

I would like to have the available call numbers in a table.我想在表格中列出可用的电话号码。

$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>';
                                    }

                                }
                            }
                            ?>

try this:尝试这个:

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

You can subtract arrays with array_diff() .您可以使用array_diff()减去数组。 In your case you could do:在你的情况下,你可以这样做:

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

You can then make a HTML table of the $availableRoepnummers .然后,您可以制作$availableRoepnummers的 HTML 表格。

use array_intersect() function like below it will retrun only matched values使用如下所示的 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));

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM