simboski19 Posted March 10, 2011 Share Posted March 10, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/230200-need-help-sorting-an-array-please-help/ Share on other sites More sharing options...
HuggieBear Posted March 10, 2011 Share Posted March 10, 2011 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>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/230200-need-help-sorting-an-array-please-help/#findComment-1185590 Share on other sites More sharing options...
simboski19 Posted March 10, 2011 Author Share Posted March 10, 2011 Thanks a million HuggieBear. It worked first time. Simon Quote Link to comment https://forums.phpfreaks.com/topic/230200-need-help-sorting-an-array-please-help/#findComment-1185620 Share on other sites More sharing options...
HuggieBear Posted March 10, 2011 Share Posted March 10, 2011 No problem, happy to help Quote Link to comment https://forums.phpfreaks.com/topic/230200-need-help-sorting-an-array-please-help/#findComment-1185627 Share on other sites More sharing options...
blirette Posted March 21, 2011 Share Posted March 21, 2011 Thanks man, used that too. Peace from Quebec City. Quote Link to comment https://forums.phpfreaks.com/topic/230200-need-help-sorting-an-array-please-help/#findComment-1190254 Share on other sites More sharing options...
blirette Posted March 21, 2011 Share Posted March 21, 2011 Is it possible to pass a value to the 'compare' function? Something like : uasort($products, 'compare("price")'); ...that would make the function reusable? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/230200-need-help-sorting-an-array-please-help/#findComment-1190255 Share on other sites More sharing options...
PFMaBiSmAd Posted March 21, 2011 Share Posted March 21, 2011 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); Quote Link to comment https://forums.phpfreaks.com/topic/230200-need-help-sorting-an-array-please-help/#findComment-1190266 Share on other sites More sharing options...
HuggieBear Posted March 21, 2011 Share Posted March 21, 2011 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>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/230200-need-help-sorting-an-array-please-help/#findComment-1190271 Share on other sites More sharing options...
blirette Posted March 21, 2011 Share Posted March 21, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/230200-need-help-sorting-an-array-please-help/#findComment-1190278 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.