Anidazen Posted July 20, 2006 Share Posted July 20, 2006 Hello.I haven't been able to find any information on this, yet I would have thought it to be a common topic. Anyway hopefully someone can help meI want to order some generated input for example by price.All I have seen on the net how to sort an associative array. I definitely need to store and sort more than two fields. Surely there must be a way to order results? Quote Link to comment Share on other sites More sharing options...
zq29 Posted July 20, 2006 Share Posted July 20, 2006 Where is your data comming from? If you're pulling from a database, you can order it at that level:[sql]SELECT * FROM `table` ORDER BY `age` DESC, `surname` ASC, `forename` ASC[/sql] Quote Link to comment Share on other sites More sharing options...
shocker-z Posted July 20, 2006 Share Posted July 20, 2006 I beleave you should simply be able to use somthing like this..SELECT * FROM table ORDER BY price, quantity DESCRegardsLiam Quote Link to comment Share on other sites More sharing options...
Anidazen Posted July 20, 2006 Author Share Posted July 20, 2006 Sorry, I should have been more specific. The data is generated inside the PHP script, there's no database involved in that way.I need to take several bits of information and order it, keeping it all together intact. Quote Link to comment Share on other sites More sharing options...
zq29 Posted July 20, 2006 Share Posted July 20, 2006 Could you give us some sample data? Quote Link to comment Share on other sites More sharing options...
Anidazen Posted July 20, 2006 Author Share Posted July 20, 2006 Sure.Basically, I am trying to produce like a mini price comparison project.I need to store an URL, seller name, price, stock status, amount - and then order it by price. Quote Link to comment Share on other sites More sharing options...
zq29 Posted July 20, 2006 Share Posted July 20, 2006 Hows this?[code]<?php$array = array(array("ashop.com","A Shop","1.20","In Stock","5"), array("bshop.com","B Shop","3.20","In Stock","3"), array("cshop.com","C Shop","2.20","In Stock","10"), array("dshop.com","D Shop","0.20","Out of Stock","8"));$tmp = array();foreach($array as $k => $v) { $tmp[$k] = $v[2];}asort($tmp);$new = array();foreach($tmp as $k => $v) { $new[] = $array[$k];}echo "<pre>"; print_r($new); echo "</pre>";?>[/code] Quote Link to comment Share on other sites More sharing options...
Anidazen Posted July 20, 2006 Author Share Posted July 20, 2006 Looks great and thanks very much for the help!That went slightly over my head - not 100% sure I can follow it, but I will trust it works.How do I get back the input? ie. first place match full set of data?Thanks very much again! Quote Link to comment Share on other sites More sharing options...
zq29 Posted July 20, 2006 Share Posted July 20, 2006 Heres a commented version:[code]<?php//An array of arrays containing our data$array = array(array("ashop.com","A Shop","1.20","In Stock","5"), array("bshop.com","B Shop","3.20","In Stock","3"), array("cshop.com","C Shop","2.20","In Stock","10"), array("dshop.com","D Shop","0.20","Out of Stock","8"));$tmp = array(); //A temporary array we will be using//Loop through our main array filling our temp array with just the prices, //setting the temp array keys the same as the keys in our main array.foreach($array as $k => $v) { $tmp[$k] = $v[2];}//Sort our temp array (of prices) from lowest to highest, asort($tmp);$new = array(); //An array that will contain all of the data in order//Loop through our temp array of prices, filling our new array with the full contents of our original array, //this time ordered by priceforeach($tmp as $k => $v) { $new[] = $array[$k];}//Show the contents of our final arrayecho "<pre>"; print_r($new); echo "</pre>";?>[/code]Here, $new[0] will allways hold the details of the entery with the cheapest price, so $new[0][1] holds the shop name, the shop URL is in $new[0][0] and $new[0][2] holds the price. Quote Link to comment Share on other sites More sharing options...
Anidazen Posted July 20, 2006 Author Share Posted July 20, 2006 I couldn't get this to work. Probably just me messing it up.Here's a snippet of code incase it helps further. I did $pricearray = array(); at the top, and then tried filling it up like this. Don't see why this failed.[code]$pricearray[7] = array("Store Name", "http://www.storeurl.com/", $useprice, $useamount, $priceper, "In Stock");$tmp = array();foreach($pricearray as $k => $v) { $tmp[$k] = $v[2];}asort($tmp);$new = array();foreach($tmp as $k => $v) { $new[] = $array[$k];}echo "Best price: <a href='" . $new[0][1] . "'>" . $new[0][0] . "</a> (" . $new[0][3] . " for \$" . $new[0][2] . ") - " . $new[0][5];[/code]Output I get is all fields I tried there blank. Any ideas? :/ Quote Link to comment Share on other sites More sharing options...
zq29 Posted July 21, 2006 Share Posted July 21, 2006 [code]<?php//You forgot to edit this line when you changed the arrays name:foreach($tmp as $k => $v) { $new[] = $array[$k];}//It needs to be:foreach($tmp as $k => $v) { $new[] = $pricearray[$k];}?>[/code] Quote Link to comment 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.