我有2个不同的结构化数组,我需要找到匹配和不匹配的元素并在表中输出结果。我找到了匹配的元素,但是我在查找不匹配的元素时遇到了麻烦。第一个数组总是比第二个数组拥有更多的元素。这是2个数组的某些部分:
$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
)
有关所需输出的更多详细信息,如果在INDIVIDUAL_PAYMNET_REFNO中找到第一个数组的元素,则应在表中显示对应的EXT_ACCOUNT_ID,然后在所有找到的匹配元素之后,放置一个空行,然后如果在第一个数组中找不到元素INDIVIDUAL_PAYMNET_REFNO,只需将值复制到同一表行即可。
我找到了匹配的值,但是我无法弄清楚如何在一个空行之后找到不匹配的元素,我从匹配的条目中获取了一些重复的条目和一些副本,这是我的代码:
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>";
欢迎对以上代码进行任何更正。谢谢。
答案 0 :(得分:0)
寻找不匹配项时,您可以使用<script>
var Arr_Records = [];
var indx = 0;
// This function is setting records in Arr_Records from another array. Its working fine (indx was initialized with 0).
function OnAddItemClick(){
Arr_Records.push([
var_ItemDetails.Id,
var_ItemDetails.Item,
var_ItemDetails.CategoryId,
var_ItemDetails.Category,
var_ItemDetails.Price,
var_ItemDetails.AvbQty,
var_ItemDetails.PurQty,
var_ItemDetails.RemQty,
var_ItemDetails.TotalPrice
]);
indx++;
OnClearClick();
}
function OnCheckOutClick(){
var payload=JSON.stringify( Arr_Records );
var ajax = new XMLHttpRequest();
ajax.onreadystatechange = function(){
if( this.readyState == 4 && this.status == 200 ){
alert( this.response );
}
}
ajax.open( 'POST', 'insertdata.php', true );
ajax.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
/* send the data as the value of a named parameter / post field */
ajax.send( 'payload='+payload );
}
</script>
<?php
/* insertdata.php */
if( $_SERVER['REQUEST_METHOD']=='POST' && !empty( $_POST['payload'] ) {
$data = filter_input( INPUT_POST, 'payload', FILTER_SANITIZE_STRING );
$json = json_decode( $data );
if( empty( json_last_error() ) ){
echo $json[0][1];
} else {
echo 'No Data Found';
}
}
?>
或array_diff
,其中array_diff_key
将显示值的不匹配项,而array_diff
将显示键的项,例如以下示例:
array_diff_key
这会给您“黄色”,因为它不匹配。
答案 1 :(得分:0)
不需要太多循环。 首先将您的工作表数组转换为普通数组
$sheet_data = [];
foreach ($sheetData as $key => $value) {
$sheet_data[] = $value[0];
}
现在,已匹配和未匹配的$ 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;
}
}
现在,您可以使用数组来显示匹配和不匹配的数据
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>";
}
不匹配的元素:
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>";
}
答案 2 :(得分:0)
您可以将$found
用作性能词典。这只是一个演示,您需要格式化输出内容。
$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/>";
}