Req #68622 [NEW]: Function to sort an array by a particular key
| From: | craig at craigfrancis dot co dot uk | Date: | Thu, 18 Dec 2014 15:49:48 +0000 |
| Subject: | Req #68622 [NEW]: Function to sort an array by a particular key | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-189110@lists.php.net to get a copy of this message | ||
From: craig at craigfrancis dot co dot uk
Operating system:
PHP version: Irrelevant
Package: Arrays related
Bug Type: Feature/Change Request
Bug description:Function to sort an array by a particular key
Description:
------------
As hinted by example 3:
http://php.net/array_multisort
I find that I'm frequently sorting arrays by a particular key
(admittedly not from the database, as that has the ORDER BY clause).
This can be done with:
function sort_name($a, $b) {
return strnatcmp($a['name'], $b['name']);
}
uasort($data, 'sort_name');
Or perhaps with PHP 5.3 anonymous functions:
uasort($data, function ($a, $b) {
return strnatcmp($a['name'], $b['name']);
});
But perhaps we could introduce a function that can reduce the amount of
code required (making it easier to read):
array_key_sort($data, 'name');
array_key_sort($data, 'name', SORT_STRING);
The example implementation below uses a class, as unfortunately my code
needs to run on a PHP 5.1 server (so no anonymous functions).
Test script:
---------------
function array_key_sort(&$array, $key, $type = 'numerical') {
$array_key_sort = new array_key_sort($key);
uasort($array, array($array_key_sort, $type));
}
class array_key_sort {
private $key = NULL;
public function __construct($key) {
$this->key = $key;
}
public function numerical($a, $b) {
if ($a[$this->key] == $b[$this->key]) {
return 0;
}
return ($a[$this->key] < $b[$this->key] ? -1 : 1);
}
public function strcmp($a, $b) {
return strcmp($a[$this->key], $b[$this->key]);
}
public function strcasecmp($a, $b) {
return strcasecmp($a[$this->key], $b[$this->key]);
}
public function strnatcmp($a, $b) {
return strnatcmp($a[$this->key], $b[$this->key]);
}
public function strnatcasecmp($a, $b) {
return strnatcasecmp($a[$this->key], $b[$this->key]);
}
}
--
Edit bug report at https://bugs.php.net/bug.php?id=68622&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=68622&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=68622&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=68622&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=68622&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=68622&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=68622&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=68622&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=68622&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=68622&r=support
Expected behavior: https://bugs.php.net/fix.php?id=68622&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=68622&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=68622&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=68622&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=68622&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=68622&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=68622&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=68622&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=68622&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=68622&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=68622&r=mysqlcfg