note 94663 modified in function.sort by danbrown

From: Date: Tue, 17 Nov 2009 15:24:24 +0000
Subject: note 94663 modified in function.sort by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-162979@lists.php.net to get a copy of this message
[EDIT BY danbrown AT php DOT net: The code provided by this anonymous author contains bugfixes for and additions to code originally donated by "toenie" on 07-NOV-09. The original author included the following note: "If you want to sort while using an 2 dimensional array, you can use this script I just wrote for myself. I thought it could be helpful for other people too."] I also added the ability to sort in ascending and descending order. $order = "ASC" will sort the array in ascending order $order = "DESC" will sort the array in descending order Here is the code: <?php function order_array_num ($array, $key, $order = "ASC") { $tmp = array(); foreach($array as $akey => $array2) { $tmp[$akey] = $array2[$key]; } if($order == "DESC") {arsort($tmp , SORT_NUMERIC );} else {asort($tmp , SORT_NUMERIC );} $tmp2 = array(); foreach($tmp as $key => $value) { $tmp2[$key] = $array[$key]; } return $tmp2; } ?> --was-- toenie's code is wrong, but the fix is easy. I also added the ability to sort in ascending and descending order. $order = "ASC" will sort the array in ascending order $order = "DESC" will sort the array in descending order Here is the code: function order_array_num ($array, $key, $order = "ASC") { $tmp = array(); foreach($array as $akey => $array2) { $tmp[$akey] = $array2[$key]; } if($order == "DESC") {arsort($tmp , SORT_NUMERIC );} else {asort($tmp , SORT_NUMERIC );} $tmp2 = array(); foreach($tmp as $key => $value) { $tmp2[$key] = $array[$key]; } return $tmp2; } http://php.net/manual/en/function.sort.php

« previous php.notes (#162979) next »