[英]Compare array elements in two different structured array
I have 2 different structured arrays and i need to find matched and mismatched elements and output result in table.我有 2 个不同的结构 arrays,我需要在表中找到匹配和不匹配的元素和 output 结果。 I've found matched elements, but im having troubles with finding mismatched ones.
我找到了匹配的元素,但我很难找到不匹配的元素。 First array always have more elements than second one.
第一个数组总是比第二个数组有更多的元素。 Heres some parts of 2 arrays:
下面是 2 arrays 的一些部分:
$sheetData = Array
(
[0] => Array
(
[0] => 01008039918
)
[1] => Array
(
[0] => 01008302495
)
[2] => Array
(
[0] => 01008603263
)
[3] => Array
(
[0] => 01008690496
)
[4] => Array
(
[0] => 01008985481
)
[5] => Array
(
[0] => 40020755400
)
[6] => Array
(
[0] => 40032435000
)
[7] => Array
(
[0] => 40231570009
)
[8] => Array
(
[0] => 40309872408
)
[9] => Array
(
[0] => 40311901009
)
[10] => Array
(
[0] => 40353576000
)
$found = Array
(
[0] => Array
(
[INDIVIDUAL_PAYMNET_REFNO] => 40594677009
[EXT_ACCOUNT_ID] => 40594677400
)
[1] => Array
(
[INDIVIDUAL_PAYMNET_REFNO] => 40595693002
[EXT_ACCOUNT_ID] => 40595693400
)
[2] => Array
(
[INDIVIDUAL_PAYMNET_REFNO] => 01008302495
[EXT_ACCOUNT_ID] => 01008309812
)
[3] => Array
(
[INDIVIDUAL_PAYMNET_REFNO] => 40690651009
[EXT_ACCOUNT_ID] => 40690651404
)
[4] => Array
(
[INDIVIDUAL_PAYMNET_REFNO] => 40625090009
[EXT_ACCOUNT_ID] => 40625090400
)
[5] => Array
(
[INDIVIDUAL_PAYMNET_REFNO] => 40708294009
[EXT_ACCOUNT_ID] => 40708294400
)
[6] => Array
(
[INDIVIDUAL_PAYMNET_REFNO] => 40499752009
[EXT_ACCOUNT_ID] => 40499752404
)
[7] => Array
(
[INDIVIDUAL_PAYMNET_REFNO] => 40604404009
[EXT_ACCOUNT_ID] => 40604404400
)
[8] => Array
(
[INDIVIDUAL_PAYMNET_REFNO] => 40425581009
[EXT_ACCOUNT_ID] => 40425581404
)
[9] => Array
(
[INDIVIDUAL_PAYMNET_REFNO] => 40440897009
[EXT_ACCOUNT_ID] => 40440897408
)
[10] => Array
(
[INDIVIDUAL_PAYMNET_REFNO] => 01008603263
[EXT_ACCOUNT_ID] => 01008610730
)
More details about the desired output, if element from first array is found in INDIVIDUAL_PAYMNET_REFNO, corresponding EXT_ACCOUNT_ID should be displayed in table, then after all found matched elements, place one empty row, then if element from first array is not found in INDIVIDUAL_PAYMNET_REFNO, just copy value to same table row.关于所需 output 的更多详细信息,如果在 INDIVIDUAL_PAYMNET_REFNO 中找到来自第一个数组的元素,则应在表中显示相应的 EXT_ACCOUNT_ID,然后在找到所有匹配的元素后,放置一个空行,然后如果在 INDIVIDUAL_PAYMNET_REFNO 中找不到来自第一个数组的元素,只需将值复制到同一表行。
Ive found matched values, but i cant figure out how to find mismatched elements after one empty row, im getting some duplicate entries and some copies from matched entries, this is my code:我找到了匹配的值,但我不知道如何在一个空行之后找到不匹配的元素,我从匹配的条目中得到一些重复的条目和一些副本,这是我的代码:
echo"<table class=\"table style=\"max-width: 50%; margin: 0 auto;\">
<thead>
<tr>
<th>INDIVIDUAL_PAYMNET_REFNO</th>
<th>EXT_ACCOUNT_ID</th>
</tr>
</thead>
<tbody>";
foreach ($sheetData as $sheet_key => $sheet_data) {
foreach ($found as $found_key => $found_val) {
if ($sheet_data[0] == $found_val['INDIVIDUAL_PAYMNET_REFNO']) {
echo "<tr>";
echo "<td>".$sheet_data[0]."</td>";
echo "<td>".$found_val['EXT_ACCOUNT_ID']."</td>";
echo "</tr>";
}
}
}
echo "<tr>";
echo "<td>"." "."</td>";
echo "<td>"." "."</td>";
echo "</tr>";
foreach ($sheetData as $sheet_key => $sheet_data) {
foreach ($found as $found_key => $found_val) {
if ($sheet_data[0] != $found_val['INDIVIDUAL_PAYMNET_REFNO'] && $found_val['EXT_ACCOUNT_ID'] == $found_val['INDIVIDUAL_PAYMNET_REFNO']) {
echo "<tr>";
echo "<td>".$sheet_data[0]."</td>";
echo "<td>".$sheet_data[0]."</td>";
echo "</tr>";
}
}
}
echo "<tbody>";
echo "</table>";
Any correction of code above is welcomed.欢迎对上述代码进行任何更正。 Thx.
谢谢。
When looking for mismatches you could use array_diff
or array_diff_key
where array_diff
will show mismatches of the values and array_diff_key
will show the keys, something like this as an example:在查找不匹配时,您可以使用
array_diff
或array_diff_key
其中array_diff
将显示值的不匹配,而array_diff_key
将显示键,例如:
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("e"=>"red","f"=>"green","g"=>"blue");
$result=array_diff($a1,$a2);
print_r($result);
This will give you "yellow" because it's a mismatch.这会给你“黄色”,因为它是不匹配的。
No need of too much loop.不需要太多的循环。 First convert your sheet array to normal array
首先将您的工作表数组转换为普通数组
$sheet_data = [];
foreach ($sheetData as $key => $value) {
$sheet_data[] = $value[0];
}
Now saperate matched and unmatched $found data:现在分开匹配和不匹配的 $found 数据:
$matched_elements = [];
$unmatched_elements = [];
foreach ($found as $found_key => $found_val) {
if (in_array($found_val['INDIVIDUAL_PAYMNET_REFNO'],$sheet_data)) {
$matched_elements[] = $found_val;
}
else {
$unmatched_elements[] = $found_val;
}
}
Now you use arrays for display matched and unmatched data现在您使用 arrays 来显示匹配和不匹配的数据
foreach ($matched_elements as $found_key => $found_val) {
echo "<tr>";
echo "<td>".$found_val[INDIVIDUAL_PAYMNET_REFNO]."</td>";
echo "<td>".$found_val['EXT_ACCOUNT_ID']."</td>";
echo "</tr>";
}
Unmatched elements:不匹配的元素:
foreach ($unmatched_elements as $found_key => $found_val) {
echo "<tr>";
echo "<td>".$found_val[INDIVIDUAL_PAYMNET_REFNO]."</td>";
echo "<td>".$found_val['EXT_ACCOUNT_ID']."</td>";
echo "</tr>";
}
You can make $found
a dictionary for performance.您可以使
$found
成为性能字典。 Here is just a demo, you need to format the output content.这里只是一个demo,需要格式化output内容。
$sheetData = array_column($sheetData,null,"INDIVIDUAL_PAYMNET_REFNO");
foreach($array as $value){
if(isset($sheetData[$value])){
echo $sheetData[$value]["EXT_ACCOUNT_ID"] . "<br/>";
}else{
$notFoundedData[] = $value;
}
}
echo "<br/>";
foreach($notFoundedData as $value){
echo $value . "<br/>";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.