redarrow Posted April 25, 2009 Share Posted April 25, 2009 if i wanted to order the array of $names_array=array("a c b","b a c","a b c"); to show in this order. 1.abc 2.acb 3.bac what i am trying to to is get the array values in order of a-z example of array <?php $names_array=array("a c b","b a c","a b c"); foreach($names_array as $test_order){ echo "$test_order\n <br>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/155612-solved-how-to-order-a-array/ Share on other sites More sharing options...
Daniel0 Posted April 25, 2009 Share Posted April 25, 2009 $names_array=array("a c b","b a c","a b c"); $names_array = array_map(create_function('$s', 'return str_replace(" ", "", $s);'), $names_array); sort($names_array); print_r($names_array); Quote Link to comment https://forums.phpfreaks.com/topic/155612-solved-how-to-order-a-array/#findComment-819004 Share on other sites More sharing options...
redarrow Posted April 25, 2009 Author Share Posted April 25, 2009 cheers dan, if i wanted to order the array of a database, and order the meta tag of description will that also work with your posted code. what it is dan, i am creating a add link, web site and i want to show all the added web sites in order of a-z of the description name. Quote Link to comment https://forums.phpfreaks.com/topic/155612-solved-how-to-order-a-array/#findComment-819010 Share on other sites More sharing options...
Daniel0 Posted April 25, 2009 Share Posted April 25, 2009 The above snippet strips all spaces from a string and then sorts it. It'll work for any array you feed it. For database rows you'd probably want to sort it in the query. Quote Link to comment https://forums.phpfreaks.com/topic/155612-solved-how-to-order-a-array/#findComment-819013 Share on other sites More sharing options...
redarrow Posted April 25, 2009 Author Share Posted April 25, 2009 Can you kindly post a example mate. sorry and thank you for your time. Quote Link to comment https://forums.phpfreaks.com/topic/155612-solved-how-to-order-a-array/#findComment-819014 Share on other sites More sharing options...
Daniel0 Posted April 25, 2009 Share Posted April 25, 2009 SELECT foo FROM bar ORDER BY baz; Quote Link to comment https://forums.phpfreaks.com/topic/155612-solved-how-to-order-a-array/#findComment-819016 Share on other sites More sharing options...
redarrow Posted April 25, 2009 Author Share Posted April 25, 2009 Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/155612-solved-how-to-order-a-array/#findComment-819018 Share on other sites More sharing options...
redarrow Posted April 25, 2009 Author Share Posted April 25, 2009 is it also passable to order two rows as one statement mate. so sorry. SELECT description,title FROM metatags ORDER BY description AND ORDER by title; Quote Link to comment https://forums.phpfreaks.com/topic/155612-solved-how-to-order-a-array/#findComment-819019 Share on other sites More sharing options...
Daniel0 Posted April 25, 2009 Share Posted April 25, 2009 That would be ORDER BY description, title. http://dev.mysql.com/doc/refman/5.0/en/select.html Quote Link to comment https://forums.phpfreaks.com/topic/155612-solved-how-to-order-a-array/#findComment-819020 Share on other sites More sharing options...
redarrow Posted April 25, 2009 Author Share Posted April 25, 2009 you are the man thank you so much just looked it up as well lol. solved 100% Quote Link to comment https://forums.phpfreaks.com/topic/155612-solved-how-to-order-a-array/#findComment-819022 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.