roddik Posted January 18, 2007 Share Posted January 18, 2007 Hello everybody! I have a 2-dimensional array, for instance, this one[pre]Array( [0] => Array ( [url] => www.php.net/ [pr] => 9 ) [1] => Array ( [url] => www.php.net/downloads.php [pr] => 8 ) [2] => Array ( [url] => en.wikipedia.org/wiki/PHP [pr] => 7 ) [8] => Array ( [url] => www.zend.com/ [pr] => 8 ))[/pre] Is there any built-in php function to sort first-level arrays by 'pr' (to make zend.com appear in the beginning)? Or, if there is not, what is the preferred way to do it? TIA Link to comment https://forums.phpfreaks.com/topic/34770-a-little-question-about-array-sorting/ Share on other sites More sharing options...
Caesar Posted January 18, 2007 Share Posted January 18, 2007 [url=http://us3.php.net/manual/en/function.asort.php]asort()[/url] or possibly [url=http://us3.php.net/manual/en/function.usort.php]usort()[/url]...depending. Link to comment https://forums.phpfreaks.com/topic/34770-a-little-question-about-array-sorting/#findComment-163895 Share on other sites More sharing options...
roddik Posted January 18, 2007 Author Share Posted January 18, 2007 WOOHOO!!! Thank you, Caesar, now everything works!!! I've even found a php.net example of sorting multi-dimensional array)))) ;D ;D[code]<?phpfunction cmp($a, $b){ return strcmp($a["fruit"], $b["fruit"]);}//Example 2. usort() example using multi-dimensional array$fruits[0]["fruit"] = "lemons";$fruits[1]["fruit"] = "apples";$fruits[2]["fruit"] = "grapes";usort($fruits, "cmp");while (list($key, $value) = each($fruits)) { echo "\$fruits[$key]: " . $value["fruit"] . "\n";}//The above example will output://$fruits[0]: apples//$fruits[1]: grapes//$fruits[2]: lemons?> [/code] Link to comment https://forums.phpfreaks.com/topic/34770-a-little-question-about-array-sorting/#findComment-163911 Share on other sites More sharing options...
Caesar Posted January 18, 2007 Share Posted January 18, 2007 No problem. Happy coding :) Link to comment https://forums.phpfreaks.com/topic/34770-a-little-question-about-array-sorting/#findComment-163917 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.