Solved

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

Posted on 2016-09-06
6
65 Views
Last Modified: 2016-09-06
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

0
Comment
Question by:tjyoung
  • 4
6 Comments
 
LVL 22

Accepted Solution

by:
Kim Walker earned 500 total points
ID: 41786232
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
 
LVL 1

Author Comment

by:tjyoung
ID: 41786239
I'm going to get my head around that right now and get back to you shortly. Sounds like a much better solution...
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 41786301
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
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
LVL 1

Author Comment

by:tjyoung
ID: 41786348
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
 
LVL 1

Author Comment

by:tjyoung
ID: 41786378
Think I got it worked...
DB::insert('INSERT INTO dplates (dplate,remove) VALUES ("'.$dplate.'",0) ON DUPLICATE KEY UPDATE remove = 0 ');
0
 
LVL 1

Author Closing Comment

by:tjyoung
ID: 41786389
That is 1000 times better solution than I was pondering.
Thanks as always!
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question