Re: compare values in 2 array
| From: | weetat | Date: | Fri, 28 Jul 2006 15:50:38 +0000 |
| Subject: | Re: compare values in 2 array | ||
| References: | 1 2 | Groups: | php.general php.general |
| Request: | Send a blank email to php-general+get-239858@lists.php.net to get a copy of this message | ||
Hi John & all,
Thanks for your info.
I have 2 array which populated from MYSQL as shown below :
My program will update status in the temporary table according to compare of 2 arrays, for example from 2 array below:
in $temptablearr = there 3 elements
in $currenttablearr = there 2 elements
If value different betweeen $temptablearr and $currenttablearr , the status is change to 'update'.
If value are same between $temptablearr and $currenttablearr , no action taken.
If there are new elements in $temptablearr and not found in $currenttablearr, the status is change to 'new'
If there are elements found in $currenttablearr and not found in $temptablearr , the status is change to 'deleted'
Anyone have any ideas or suggestion how to do this ? Thanks for your help.
$temptablearr = array(0 =>
array("chassis_serial_no"=>"1235",
"country"=>"Malaysia",
"card_serial_no"=>"cd-12345",
"card_model"=>"ws8950"),
1 =>
array("chassis_serial_no"=>"1235",
"country"=>"Malaysia",
"card_serial_no"=>"cd-890",
"card_model"=>"ws1234"),
2 =>
array("chassis_serial_no"=>"8888",
"country"=>"Indonesia",
"card_serial_no"=>"cd-12345",
"card_model"=>"ws999"),
);
$currenttablearr = array( 0 =>
array("chassis_serial_no"=>"1235",
"country"=>"Malaysia",
"card_serial_no"=>"cd-12345",
"card_model"=>"ws8950"),
1=>
array("chassis_serial_no"=>"1235",
"country"=>"Singapore",
"card_serial_no"=>"cd-890",
"card_model"=>"ws1234"),
);
Below is my test code .
<?php
$temptablearr = array(0 =>
array("chassis_serial_no"=>"1235",
"country"=>"Malaysia",
"card_serial_no"=>"cd-12345",
"card_model"=>"ws8950"),
1 =>
array("chassis_serial_no"=>"1235",
"country"=>"Malaysia",
"card_serial_no"=>"cd-890",
"card_model"=>"ws1234"),
2 =>
array("chassis_serial_no"=>"8888",
"country"=>"Indonesia",
"card_serial_no"=>"cd-12345",
"card_model"=>"ws999"),
);
$currenttablearr = array( 0 =>
array("chassis_serial_no"=>"1235",
"country"=>"Malaysia",
"card_serial_no"=>"cd-12345",
"card_model"=>"ws8950"),
1=>
array("chassis_serial_no"=>"1235",
"country"=>"Singapore",
"card_serial_no"=>"cd-890",
"card_model"=>"ws1234"),
);
$compare = true;
foreach($temptablearr as $key => $val)
{
if($currenttablearr[$key] != $val) {
$compare = false;} if($compare === false) {
echo 'update status in table to update<br><br>';}else if($compare === true){
echo 'skip, do not shown in page.<br><br>';} } ?> John Wells wrote:
On 7/28/06, weetat <weetat.yeo@cxrus.com> wrote:I need to compare values in array which populated by database , below is the code :Without mentioning how your code performs some unnecessary actions, and is also full of errors, here's the idea of what you want (untested): [code] // assume at first they are same $compare = true; // compare $array1 and $array2, assuming they have same structure foreach($array1 as $key => $val) { // value doesn't match, so set $compare to false and break out of for loop if($array2[$key] != $val) {$compare = false; break;} } if($compare === false) { echo 'arrays did not match.'; } [/code] HTH, John W