Jump to content

Another array problem - using natsort($array)


murfitUK

Recommended Posts

I need to use the natsort function to sort an array.I get the result from a query using$query = "select * from table", then$result = mysql_query($query)thenwhile ($row=mysql_fetch_array($result)) { print $row['item_id'] . $row['title'] ... etc }The item_id field is mainly numbers but some have letters so are treated as text. If I put ORDER BY item_id in the query the results are returned like this:11111112223 etc (instead of 1, 2, 11, 12, 23, 111...)The natsort is supposed to sort this out but I can't figure out how to get the $result into an array that can then be natsort'ed and then put into the while loop.Unless there is a special ORDER BY that I can use on the mySql query which would save a lot of problems?Can anyone help? Thanks.

Link to comment
Share on other sites

You can build the array like this:

$ids = array();$titles = array();while ($row=mysql_fetch_array($result)){  $ids[] = $row['item_id'];  $titles[] = $row['title'];}

If you use natsort on the ids array and then use foreach to loop through it and use the key from foreach to get the title from the titles array then the titles and ids will still match up even though the arrays are in different orders.

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...