Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2016-09-06
6
Medium Priority
?
104 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
6 Comments
 
LVL 22

Accepted Solution

by:
Kim Walker earned 2000 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 111

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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

618 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