0

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
*/
Marteyn
  • 17
  • 8
  • Own the luxury to yourself to setup a step debugger and then step-debug through the code so that you can watch and inspect what actually happens. Then you can validate your expectations and if there is a situation that surprises you, you can easily check all the variables in question in place. https://xdebug.org/ – hakre Jun 25 '21 at 22:33

1 Answers1

0

Use simple array_filter() method:

$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);

The output will be:

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 
) 
)
Faizan Ali
  • 297
  • 2
  • 9
  • Thanks Faizan, I've looked into that as well and if all else fails I'll have to rewrite the code accordingly. But what I'm really after is to understand why the function's (is_null($svalue)) section isn't returning "True", despite the values from both arrays being 'Null'. I just can't grasp why in that situation the function doesn't work. – Marteyn Jun 25 '21 at 17:07
  • In my knowledge, If a variable is specifically assigned to constant NULL then `is_null()` function working well otherwise return false. Example: $`test = NULL; is_null($test)` it will return true. – Faizan Ali Jun 25 '21 at 17:58
  • If variable `$test = ' ';` in this case `is_null()` will return false. But variable is empty. – Faizan Ali Jun 25 '21 at 18:04
  • `NULL' is specific constant type value in php. We can't check if variable is empty value with `is_null()`. is_null() working fine only when we specifically assigned variable a value `NULL. Like this one `$test = NULL`. – Faizan Ali Jun 25 '21 at 18:07
  • Check this link you will understand` https://stackoverflow.com/questions/5615747/what-is-the-difference-between-null-and-empty#:~:text=9%20Answers&text=A%20variable%20is%20NULL%20if,empty%2C%20but%20is%20not%20NULL%20.` – Faizan Ali Jun 25 '21 at 18:09
  • Thanks for that, Faizan.. I'm gonna experiment based on your feedback. – Marteyn Jun 30 '21 at 05:19