Jump to content

Need help sorting an array - please help


simboski19

Recommended Posts

I am attempting to sort an array by price but cannot seem to get php to do this. Below is the array I am attempting to sort by the value ~ price:

 

Array

(

    [http://pdt.tradedoubler.com/click?a(1774061)p(19045)prod(550992137)ttid(5)url(http%3A%2F%2Fwww.halfords.com%2Fwebapp%2Fwcs%2Fstores%2Fservlet%2Fproduct_storeId_10001_catalogId_10151_categoryId_165488_productId_561801_langId_-1%3Fsource%3Dtd)] => Array

        (

            [logo] => nc0iicjxmq.gif

            [stock] => In Stock  In Stock

            [price] => 14.99

            [delivery] => 4.99

            => http://pdt.tradedoubler.com/click?a(1774061)p(19045)prod(550992137)ttid(5)url(http%3A%2F%2Fwww.halfords.com%2Fwebapp%2Fwcs%2Fstores%2Fservlet%2Fproduct_storeId_10001_catalogId_10151_categoryId_165488_productId_561801_langId_-1%3Fsource%3Dtd)

            [mname] => Halfords

        )

 

    [http://pdt.tradedoubler.com/click?a(1774061)p(19045)prod(767538675)ttid(5)url(http%3A%2F%2Fwww.halfords.com%2Fwebapp%2Fwcs%2Fstores%2Fservlet%2Fproduct_storeId_10001_catalogId_10151_categoryId_165497_productId_780417_langId_-1%3Fsource%3Dtd)] => Array

        (

            [logo] => nc0iicjxmq.gif

            [stock] => In Stock  In Stock

            [price] => 19.99

            [delivery] => 4.99

            => http://pdt.tradedoubler.com/click?a(1774061)p(19045)prod(767538675)ttid(5)url(http%3A%2F%2Fwww.halfords.com%2Fwebapp%2Fwcs%2Fstores%2Fservlet%2Fproduct_storeId_10001_catalogId_10151_categoryId_165497_productId_780417_langId_-1%3Fsource%3Dtd)

            [mname] => Halfords

        )

 

    [http://pdt.tradedoubler.com/click?a(1774061)p(19045)prod(254177017)ttid(5)url(http%3A%2F%2Fwww.halfords.com%2Fwebapp%2Fwcs%2Fstores%2Fservlet%2Fproduct_storeId_10001_catalogId_10151_categoryId_165497_productId_271987_langId_-1%3Fsource%3Dtd)] => Array

        (

            [logo] => nc0iicjxmq.gif

            [stock] => In Stock  In Stock

            [price] => 15.00

            [delivery] => 2.99

            => http://pdt.tradedoubler.com/click?a(1774061)p(19045)prod(254177017)ttid(5)url(http%3A%2F%2Fwww.halfords.com%2Fwebapp%2Fwcs%2Fstores%2Fservlet%2Fproduct_storeId_10001_catalogId_10151_categoryId_165497_productId_271987_langId_-1%3Fsource%3Dtd)

            [mname] => Halfords

        )

 

    [http://pdt.tradedoubler.com/click?a(1774061)p(19045)prod(550992487)ttid(5)url(http%3A%2F%2Fwww.halfords.com%2Fwebapp%2Fwcs%2Fstores%2Fservlet%2Fproduct_storeId_10001_catalogId_10151_categoryId_165488_productId_561805_langId_-1%3Fsource%3Dtd)] => Array

        (

            [logo] => nc0iicjxmq.gif

            [stock] => In Stock  In Stock

            [price] => 4.99

            [delivery] => 2.99

            => http://pdt.tradedoubler.com/click?a(1774061)p(19045)prod(550992487)ttid(5)url(http%3A%2F%2Fwww.halfords.com%2Fwebapp%2Fwcs%2Fstores%2Fservlet%2Fproduct_storeId_10001_catalogId_10151_categoryId_165488_productId_561805_langId_-1%3Fsource%3Dtd)

            [mname] => Halfords

        )

 

    [http://pdt.tradedoubler.com/click?a(1774061)p(19045)prod(18823539)ttid(5)url(http%3A%2F%2Fwww.halfords.com%2Fwebapp%2Fwcs%2Fstores%2Fservlet%2Fproduct_storeId_10001_catalogId_10151_categoryId_165488_productId_155878_langId_-1%3Fsource%3Dtd)] => Array

        (

            [logo] => nc0iicjxmq.gif

            [stock] => In Stock  In Stock

            [price] => 18.00

            [delivery] => 4.99

            => http://pdt.tradedoubler.com/click?a(1774061)p(19045)prod(18823539)ttid(5)url(http%3A%2F%2Fwww.halfords.com%2Fwebapp%2Fwcs%2Fstores%2Fservlet%2Fproduct_storeId_10001_catalogId_10151_categoryId_165488_productId_155878_langId_-1%3Fsource%3Dtd)

            [mname] => Halfords

        )

 

    [http://pdt.tradedoubler.com/click?a(1774061)p(19045)prod(580777789)ttid(5)url(http%3A%2F%2Fwww.halfords.com%2Fwebapp%2Fwcs%2Fstores%2Fservlet%2Fproduct_storeId_10001_catalogId_10151_categoryId_242553_productId_385055_langId_-1%3Fsource%3Dtd)] => Array

        (

            [logo] => nc0iicjxmq.gif

            [stock] => In Stock  In Stock

            [price] => 21.99

            [delivery] => Free Shipping

            => http://pdt.tradedoubler.com/click?a(1774061)p(19045)prod(580777789)ttid(5)url(http%3A%2F%2Fwww.halfords.com%2Fwebapp%2Fwcs%2Fstores%2Fservlet%2Fproduct_storeId_10001_catalogId_10151_categoryId_242553_productId_385055_langId_-1%3Fsource%3Dtd)

            [mname] => Halfords

        )

 

    [http://pdt.tradedoubler.com/click?a(1774061)p(19045)prod(18823503)ttid(5)url(http%3A%2F%2Fwww.halfords.com%2Fwebapp%2Fwcs%2Fstores%2Fservlet%2Fproduct_storeId_10001_catalogId_10151_categoryId_165497_productId_217063_langId_-1%3Fsource%3Dtd)] => Array

        (

            [logo] => nc0iicjxmq.gif

            [stock] => In Stock  In Stock

            [price] => 24.99

            [delivery] => Reserve and Collect

            => http://pdt.tradedoubler.com/click?a(1774061)p(19045)prod(18823503)ttid(5)url(http%3A%2F%2Fwww.halfords.com%2Fwebapp%2Fwcs%2Fstores%2Fservlet%2Fproduct_storeId_10001_catalogId_10151_categoryId_165497_productId_217063_langId_-1%3Fsource%3Dtd)

            [mname] => Halfords

        )

 

    [http://track.webgains.com/click.html?wgcampaignid=72782&wgprogramid=2730&product=1&wglinkid=90005&productname=Continental+Sport+Contact+Road%2FHybrid+Bicycle+Tyre&wgtarget=http://www.awcycles.co.uk/brands/Continental/Sport_Contact_Road/Hybrid_Bicycle_Tyre] => Array

        (

            [logo] => 8yfd588e8t.jpg

            [stock] => In Stock  In Stock

            [price] => 21.99

            [delivery] =>

            => http://track.webgains.com/click.html?wgcampaignid=72782&wgprogramid=2730&product=1&wglinkid=90005&productname=Continental+Sport+Contact+Road%2FHybrid+Bicycle+Tyre&wgtarget=http://www.awcycles.co.uk/brands/Continental/Sport_Contact_Road/Hybrid_Bicycle_Tyre

            [mname] => AW Cycles

        )

 

)

 

Any help would be much appreciated.

 

Thanks

Simon

Link to comment
https://forums.phpfreaks.com/topic/230200-need-help-sorting-an-array-please-help/
Share on other sites

You need to perform a comparison in your own function.  Here's my code that sorts by price ascending.

<?php
// Setup product array (You already have yours)
$products = array(
                'Bike' => array('price' => '199.99'),
                'Apple' => array('price' => '0.87'),
                'Car' => array('price' => '5999.00')
            );

// Comparison function (swap the 1 and -1 around to sort descending)            
function compare($x, $y){
    if ( $x['price'] == $y['price'] ){
        return 0;
    }
    else if ( $x['price'] < $y['price'] ){
        return -1;
    }
    else {
        return 1;
    }
}

// Sort the products
uasort($products, 'compare');

// Output the products
echo '<pre>';
print_r($products);
echo '</pre>';

?>

  • 2 weeks later...

For a general purpose method, you would use array_multisort().

 

Assuming that $data is an array of arrays and you wanted to sort by the 'price' field -

 

$sort_field = 'price';
foreach ($data as $key => $row) {
    $sort_by_this[$key]  = $row[$sort_field];
}

array_multisort($sort_by_this, SORT_ASC, $data);

 

You can't pass in additional arguments to this function as it's a callback of uasort(), but you could use global variables.  See the example below.  I've added two new variables, sort order and sort key.  I've also added quantity to the product array as a new key to sort on.

<?php

// Setup product array (You already have yours)
$products = array(
                'Bike' => array('price' => '199.99','quantity' => '1'),
                'Apple' => array('price' => '0.87','quantity' => '2'),
                'Car' => array('price' => '5999.00','quantity' => '3')
            );
            
// *NEW* Specify either ASC or DESC
$sortorder = 'DESC';

// *NEW* Specify the key you'd like the array sorted on
$sortkey = 'quantity';
            
// Comparison function *NOTE* You now change the sort order with the global variable, not by swaping the return values around
function compare($x, $y){
    global $sortorder;
    global $sortkey;
    
    if ( $x[$sortkey] == $y[$sortkey] ){
        $return = 0;
    }
    if ($sortorder == 'ASC'){
       return ($x[$sortkey] < $y[$sortkey]) ? -1 : 1;
    }
    else {
       return ($x[$sortkey] < $y[$sortkey]) ? 1 : -1;
    }
}

uasort($products, 'compare');

// Output the products
echo '<pre>';
print_r($products);
echo '</pre>';
?>

Thanks PFM. I had a similar function that had a bug in it. Yours works great!

 

Here's the buggy code I had :

// tableau temporaire pour trier les valeurs
$aTableauTemporaire = array();

// boucler sur toutes les lignes de $aProduits
foreach($aProduits as &$aProduit)
{
// 
$aTableauTemporaire[] = &$aProduit["intNbReservation"];
};

// trier par ordre descendant
array_multisort($aTableauTemporaire, SORT_DESC, $aProduits);

Thanks to HuggieBear too! ... but if I don't need globals, I'll prefer the other way shown above.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.