简体   繁体   中英

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. 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:

$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.

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>"."&nbsp;"."</td>";
   echo "<td>"."&nbsp;"."</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:

$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:

$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

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. Here is just a demo, you need to format the output content.

$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/>";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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