Solved

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

Posted on 2016-09-06
6
85 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 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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 …

707 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