At 11:18 PM 7/28/2006, weetat wrote:
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 elements in the $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.
...
$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>';
}
}
Weetat,
You might consider concatenating all the elements in each sub-array
to make the comparison logic simpler, e.g.:
foreach ($currenttablearr as $key => $subarray)
{
$comparisonArray[$key] = explode("_", $subarray);
}
This would result in:
$comparisonArray[0] == "1235_Malaysia_cd-12345_ws8950"
$comparisonArray[1] == "1235_Singapore_cd-890_ws1234"
If you do that for both arrays you're comparing then it requires less
complicated code to discover which values in one array don't exist in
the other.
Paul
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php