Re: different value in array

From: Date: Sat, 29 Jul 2006 07:20:14 +0000
Subject: Re: different value in array
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-239869@lists.php.net to get a copy of this message
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

« previous php.general (#239869) next »