Re: sorting in array

From: Date: Mon, 31 Jul 2006 06:56:46 +0000
Subject: Re: sorting in array
References: 1 2  Groups: php.general php.general 
Request: Send a blank email to php-general+get-239891@lists.php.net to get a copy of this message
Hi , Doesn't work . Any ideas ? Thanks Peter Lauri wrote:
function cmpcountry($a, $b) {
         $country1 = $a['country'];
         $country2 = $b['country'];
if($country1=='') return 1; else return ($country1 < $country2) ? -1 : 1; } -----Original Message----- From: weetat [mailto:weetat.yeo@cxrus.com] Sent: Monday, July 31, 2006 12:32 PM To: php-general@lists.php.net Subject: [PHP] sorting in array Hi all , I have array value as shown below, i have paste my test php code below: I have problem when doing usort() when 'country' = '', i would like to display records where country = '' last. Any ideas how to do that ? Thanks $arraytest= array(
      array
         (
             'country' => '',
         )
      ,
      array
         (
             'country' => 'Thailand',
         )
      ,
      array
         (
             'country' => 'Singapore',
         )
      ,
      array
         (
             'country' => 'Singapore',
         )
      ,
      array
         (
             'country' => '',
         )
       ,
array
         (
             'country' => '',
         )
, array
         (
             'country' => '',
         )
      );
function cmpcountry($a, $b) {
         $country1 = $a['country'];
         $country2 = $b['country'];
          return ($country1 < $country2) ? -1 : 1;
}
     usort($arraytest,"cmpcountry");
     while(list($name,$value)=each($arraytest)){
echo $name."<br><br>"; while(list($n,$v) = each($arraytest[$name])){
	       echo $v."<br><br>";
}
      }


« previous php.general (#239891) next »