I have two arrays, one loaded from a .cvs file and one from a db table. The idea is to match and remove any duplicate entries from the db array and end up with a list of db records that need to be removed from the table.
However, it seems that when 'item_code' is an empty value or NULL, there is no match. I've tried every combination of == "", === null, is_null(), and empty() that I could think of in the function as well as in the code, but I get keeping the same result.
It's probably something very obvious, but I can't for the life of me see what the issue is here. Anyone mind having a go?
The two arrays are as follows:
$arr_from_csv = array(
array(
"item_code" => '',
"debtor_code" => '3CAS0001',
"agent" => 'CCCS-N',
"doc_date" => '2021-06-05',
"total_value" => '576.00',
"total_quantity" => '1.0000'
),
array(
"item_code" => null,
"debtor_code" => '3CAS0001',
"agent" => 'CCCS-N',
"doc_date" => '2021-06-14',
"total_value" => '290.00',
"total_quantity" => '2.0000'
),
array(
"item_code" => "FMDU - 100 (BIO)",
"debtor_code" => "3BER0001",
"agent" => "VLHZ",
"doc_date" => "2021-06-01",
"total_value" => "1482.37",
"total_quantity" => "168.0000"
),
array(
"item_code" => '',
"debtor_code" => '3CAS0001',
"agent" => 'CCCS-N',
"doc_date" => '2021-06-21',
"total_value" => '151.02',
"total_quantity" => '9.0000'
)
);
$arr_from_db = array(
array(
"id" => "3738",
"item_code" => NULL,
"debtor_code" => "3CAS0001",
"agent" => "CCCS-N",
"doc_date" => "2021-06-05",
"total_value" => "576.00",
"total_quantity" => "1.0000",
"date_added" => "2021-06-10",
"last_updated_date" => "0000-00-00 00:00:00",
"last_updated_user" => "0",
"filename" => "dashboard_20210610-0922_saledata.csv"
),
array(
"id" => "3787",
"item_code" => NULL,
"debtor_code" => "3CAS0001",
"agent" => "CCCS-N",
"doc_date" => "2021-06-14",
"total_value" => "290.00",
"total_quantity" => "2.0000",
"date_added" => "2021-06-14",
"last_updated_date" => "2021-06-14 21:54:34",
"last_updated_user" => "0",
"filename" => "dashboard_20210614-1356_saledata.csv"
),
array(
"id" => "3664",
"item_code" => "FMDU - 100 (BIO)",
"debtor_code" => "3BER0001",
"agent" => "VLHZ",
"doc_date" => "2021-06-01",
"total_value" => "1482.37",
"total_quantity" => "168.0000",
"date_added" => "2021-06-10",
"last_updated_date" => "0000-00-00 00:00:00",
"last_updated_user" => "0",
"filename" => "dashboard_20210610-0922_saledata.csv"
),
array(
"id" => "3975",
"item_code" => NULL,
"debtor_code" => "3CAS0001",
"agent" => "CCCS-N",
"doc_date" => "2021-06-21",
"total_value" => "151.02",
"total_quantity" => "9.0000",
"date_added" => "2021-06-21",
"last_updated_date" => "2021-06-22 09:41:53",
"last_updated_user" => "0",
"filename" => "dashboard_20210622-0932_saledata.csv"
),
array(
"id" => "4009",
"item_code" => "FGT- LG300g",
"debtor_code" => "3HON0001",
"agent" => "CCCS-N",
"doc_date" => "2021-06-21",
"total_value" => "-50.58",
"total_quantity" => "-1.0000",
"date_added" => "2021-06-22",
"last_updated_date" => "2021-06-22 09:41:53",
"last_updated_user" => "0",
"filename" => "dashboard_20210622-0932_saledata.csv"
)
);
The function I use to do the search :
function multidimensional_search($parents, $searched) {
if (empty($searched) || empty($parents)) {
return false;
}
foreach ($parents as $key => $value) {
$exists = true;
foreach ($searched as $skey => $svalue) {
if (is_null($svalue)) {
$exists = ($exists && IsSet($parents[$key][$skey]) && $parents[$key][$skey] === null);
}
else {
$exists = ($exists && IsSet($parents[$key][$skey]) && $parents[$key][$skey] == $svalue);
}
}
if($exists){ return $key; }
}
return false;
}
And finally, the code
foreach ($arr_from_csv as $ikey => $ivalue) {
$matchkey = multidimensional_search($arr_from_db, $ivalue);
if(!$matchkey) {
echo "Key [" . $ikey . "] not found.<br>";
//echo "<pre>"; var_dump($ivalue); echo "</pre>";
}
else {
echo "Key [" . $ikey . "] found.<br>";
unset($arr_from_db[$matchkey]);
}
}
echo "REMAINING ARRAY CONTAINS " . count($arr_from_db);
//echo "<pre>"; var_dump($arr_from_db); echo "</pre>";
/*
RESULT:
Key [0] not found.
Key [1] not found.
Key [2] found.
Key [3] not found.
REMAINING ARRAY CONTAINS 4
*/