Using usort() to Sort a Multidimensional Array by One of Its Fields : Array sort : Data Structure PHP Source Code


PHP Source Code » Data Structure » Array sort »

 

Using usort() to Sort a Multidimensional Array by One of Its Fields






<?php
$products = arrayarrayname=>"A", price=>4.5  ),
                   arrayname=>"C", price=>5.5  ),
                   arrayname=>"D", price=>2.5  ),
                   arrayname=>"B", price=>2.5  )
);

function priceCmp$a, $b ){
    if $a[price== $b[price] )
        return 0;
    if $a[price< $b[price] )
         return -1;
    return 1;
}
usort$products, priceCmp );
foreach $products as $val )
     print "$val[name]: $val[price]<BR>n";
?>


           
       



HTML code for linking to this page:

Follow Navioo On Twitter

PHP Source Code

 Navioo Data Structure
» Array sort