note 46278 added to function.array-multisort
| From: | meddleatdzygn dot com at osu1 dot php dot net | Date: | Tue, 05 Oct 2004 14:40:09 +0000 |
| Subject: | note 46278 added to function.array-multisort | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-78026@lists.php.net to get a copy of this message | ||
If you want to sort a multidomensional array by key name you cannot use array_multisort. ie: for an
array named $archivos that prints like this:
Array
(
[0] => Array
(
[index] => 0
[name] => test
)
[1] => Array
(
[index] => 0
[name] => watertaxi.jpg
)
[2] => Array
(
[index] => 0
[name] => 2_0003.JPG
)
[3] => Array
(
[index] => 0
[name] => 24A_0025.JPG
)
[4] => Array
(
[index] => 1
[name] => _CIMG3501.JPG
)
)
If I wanted to order by "name" I'd use:
function comparar($a, $b) {
return strnatcasecmp($a["name"], $b["name"]);
}
usort($archivos, "comparar");
This function performs a case insensitive string comparison using a "natural order"
algorithm (strnatcasecmp), resulting in:
Array
(
[0] => Array
(
[index] => 0
[name] => 2_0003.JPG
)
[1] => Array
(
[index] => 0
[name] => 24A_0025.JPG
)
[2] => Array
(
[index] => 0
[name] => test
)
[3] => Array
(
[index] => 0
[name] => watertaxi.jpg
)
[4] => Array
(
[index] => 1
[name] => _CIMG3501.JPG
)
)
----
Manual Page -- http://www.php.net/manual/en/function.array-multisort.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+46278
Delete -- http://master.php.net/manage/user-notes.php?action=delete+46278&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+46278&report=yes
Search -- http://master.php.net/manage/user-notes.php