PHP 如果值为空或 NULL,多维数组键搜索和值函数不工作
PHP multidimensional array search by key and value funtion not working if value is empty or NULL
我有两个数组,一个从 .cvs 文件加载,一个从 db table 加载。这个想法是匹配并删除数据库数组中的任何重复条目,最后得到需要从 table.
中删除的数据库记录列表
但是,当'item_code'为空值或NULL时,似乎没有匹配。我在函数和代码中尝试了 == ""、=== null、is_null() 和 empty() 的所有组合,但我保持不变结果。
这可能是非常明显的事情,但我无法在我的生活中看到这里的问题。有人介意试一试吗?
两个数组如下:
$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"
)
);
我用来做搜索的函数:
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;
}
最后,代码
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
*/
使用简单的array_filter()
方法:
$arr_from_db = array(
array(
"id" => "3738",
"item_code" => '',
"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" => "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"
)
);
$test = array_filter($arr_from_db, function ($record){
return $record['item_code'] != '';
});
print_r($test);
输出将是:
Array (
[1] => 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
)
)
我有两个数组,一个从 .cvs 文件加载,一个从 db table 加载。这个想法是匹配并删除数据库数组中的任何重复条目,最后得到需要从 table.
中删除的数据库记录列表但是,当'item_code'为空值或NULL时,似乎没有匹配。我在函数和代码中尝试了 == ""、=== null、is_null() 和 empty() 的所有组合,但我保持不变结果。
这可能是非常明显的事情,但我无法在我的生活中看到这里的问题。有人介意试一试吗?
两个数组如下:
$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"
)
);
我用来做搜索的函数:
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;
}
最后,代码
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
*/
使用简单的array_filter()
方法:
$arr_from_db = array(
array(
"id" => "3738",
"item_code" => '',
"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" => "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"
)
);
$test = array_filter($arr_from_db, function ($record){
return $record['item_code'] != '';
});
print_r($test);
输出将是:
Array (
[1] => 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
)
)