Trying to compare to slightly different arrays and act on the results...

Hi,
I have an array ( vehicle demo license plates that the dealer has in-store) that is submitted via a form (1st one below) and I'm trying to compare it to the results of a query out of laravel  (second code block)

My goal is:
1) compare the 2 arrays (somehow being they are slightly different)
2) those dplates that only exist in the DB query need to be removed
3) those dplates that only exist in the form submission need to be inserted
4) those that exist in both arrays need to be left untouched

Any idea how to accomplish this?

array:9 [▼
  0 => "D18750"
  1 => "D19221"
  2 => "D19224"
  3 => "D20654"
  4 => "D20880"
  5 => "D20881"
  6 => "D21275"
  7 => "D21276"
]

Open in new window

array:8 [▼
  0 => {#380 ▼
    +"dplate": "D19221"
  }
  1 => {#381 ▼
    +"dplate": "D19224"
  }
  2 => {#382 ▼
    +"dplate": "D21275"
  }
  3 => {#383 ▼
    +"dplate": "D20654"
  }
  4 => {#384 ▼
    +"dplate": "D18750"
  }
  5 => {#385 ▼
    +"dplate": "D20880"
  }
  6 => {#386 ▼
    +"dplate": "D21276"
  }
  7 => {#387 ▼
    +"dplate": "D20881"
  }
]

Open in new window

LVL 1
tjyoungAsked:
Who is Participating?
 
Kim WalkerWeb Programmer/TechnicianCommented:
I would do this in the database. I would include a Boolean field named remove or something pertinent and the dplate field to be unique. Before I perform the insert/update, I would set this field on all rows to be true (1).
UPDATE `table` SET `remove`=1 WHERE 1

Open in new window

Then I would insert the dplate values from the form submission with an ON DUPLICATE KEY UPDATE clause.
INSERT INTO `table` (`dplate`,`remove`) VALUES ("$dplate",0) ON DUPLICATE KEY UPDATE `remove`=0

Open in new window

This query will insert all the new dplate values and change the remove column to false (0) if it already exists. After the insert/update query is complete, remove all the rows where the remove column is true.
DELETE FROM `table` WHERE `remove`=1

Open in new window

0
 
tjyoungAuthor Commented:
I'm going to get my head around that right now and get back to you shortly. Sounds like a much better solution...
0
 
Ray PaseurCommented:
I think Kim's suggestion is probably the best.  But as a theoretical matter here is a script that would find the differences -- if there were any differences.  These collections are the same.
https://iconoun.com/demo/temp_tjyoung.php
<?php // demo/temp_tjyoung.php
/**
 * https://www.experts-exchange.com/questions/28967952/Trying-to-compare-to-slightly-different-arrays-and-act-on-the-results.html
 *
 * http://php.net/manual/en/function.array-diff.php
 * https://www.experts-exchange.com/articles/22519/Understanding-JSON-in-PHP-and-JavaScript-Applications.html
 */
error_reporting(E_ALL);
echo '<pre>';


$post_array =
[ 0 => "D18750"
, 1 => "D19221"
, 2 => "D19224"
, 3 => "D20654"
, 4 => "D20880"
, 5 => "D20881"
, 6 => "D21275"
, 7 => "D21276"
]
;

$db_json =
'[ {"dplate": "D19221"}
,  {"dplate": "D19224"}
,  {"dplate": "D21275"}
,  {"dplate": "D20654"}
,  {"dplate": "D18750"}
,  {"dplate": "D20880"}
,  {"dplate": "D21276"}
,  {"dplate": "D20881"}
]'
;

$db_obj = json_decode($db_json);
$db_array = [];
foreach ($db_obj as $obj)
{
    $db_array[] = (string)$obj->dplate;
}

// those dplates that only exist in the DB query need to be removed
$db_only = array_diff($db_array, $post_array);

// those dplates that only exist in the form submission need to be inserted
$post_only = array_diff($post_array, $db_array);

// SHOW WHAT WE'VE GOT
echo PHP_EOL . 'POST: ';
var_dump($post_array);
echo PHP_EOL;

echo PHP_EOL . 'DB: ';
var_dump($db_array);
echo PHP_EOL;

echo PHP_EOL . 'those dplates that only exist in the DB query need to be removed: ';
var_dump($db_only);
echo PHP_EOL;

echo PHP_EOL . 'those dplates that only exist in the form submission need to be inserted: ';
var_dump($post_only);
echo PHP_EOL;

Open in new window

Outputs:
POST: array(8) {
  [0]=>
  string(6) "D18750"
  [1]=>
  string(6) "D19221"
  [2]=>
  string(6) "D19224"
  [3]=>
  string(6) "D20654"
  [4]=>
  string(6) "D20880"
  [5]=>
  string(6) "D20881"
  [6]=>
  string(6) "D21275"
  [7]=>
  string(6) "D21276"
}


DB: array(8) {
  [0]=>
  string(6) "D19221"
  [1]=>
  string(6) "D19224"
  [2]=>
  string(6) "D21275"
  [3]=>
  string(6) "D20654"
  [4]=>
  string(6) "D18750"
  [5]=>
  string(6) "D20880"
  [6]=>
  string(6) "D21276"
  [7]=>
  string(6) "D20881"
}


those dplates that only exist in the DB query need to be removed: array(0) {
}


those dplates that only exist in the form submission need to be inserted: array(0) {
}

Open in new window

1
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
tjyoungAuthor Commented:
HI,
I suspect there is something wrong with my raw insert?
update to '1' works great
certainly 'delete' worked
Not sure about the raw part though? The preg_replace is just to clean up any lowercase/dashes etc.

DB::table('dplates')
    	->where('dealership_id',$dealership_id)
    	->update(['remove' => 1]);
    	
    	foreach($dplates as $dplate){
	$dplate = preg_replace("/[^A-Za-z0-9 ]/", "", strtoupper($dplate));
	DB::raw('INSERT INTO `dplates` (`dplate`,`remove`) VALUES ("$dplate",0) ON DUPLICATE KEY UPDATE `remove` = 0 ');
		}
		
		DB::table('dplates')
		->where('dealership_id',$dealership_id)
		->where('remove', 1)
		->delete();

Open in new window

db pic
0
 
tjyoungAuthor Commented:
Think I got it worked...
DB::insert('INSERT INTO dplates (dplate,remove) VALUES ("'.$dplate.'",0) ON DUPLICATE KEY UPDATE remove = 0 ');
0
 
tjyoungAuthor Commented:
That is 1000 times better solution than I was pondering.
Thanks as always!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.