note 92122 modified in function.asort by danbrown
| From: | danbrown@php.net | Date: | Thu, 09 Jul 2009 20:32:04 +0000 |
| Subject: | note 92122 modified in function.asort by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-158124@lists.php.net to get a copy of this message | ||
<?php
// Sort an associative array while case insensitive
// Might be useful to someone else
//
// Example:
// $arr = $res1 = array(
// [a] => Trans Union
// [b] => TRACE
// [c] => TeleMatch
// [d] => test);
//
// asort($res1);
// $res2 = asorti($arr);
//
// ASORT RESULT
// $res1 = array(
// [b] => TRACE
// [c] => TeleMatch
// [a] => Trans Union
// [d] => test);
//
// DESIRED RESULT
// $res2 = array(
// [c] => TeleMatch
// [d] => test
// [b] => TRACE
// [a] => Trans Union);
function asorti($arr) {
$arr2 = $arr;
foreach($arr2 as $key => $val) {
$arr2[$key] = strtolower($val);
}
asort($arr2);
foreach($arr2 as $key => $val) {
$arr2[$key] = $arr[$key];
}
return $arr2;
}
?>
--was--
// Sort an associative array while case insensitive
// Might be useful to someone else
//
// Example:
// $arr = $res1 = array(
// [a] => Trans Union
// [b] => TRACE
// [c] => TeleMatch
// [d] => test);
//
// asort($res1);
// $res2 = asorti($arr);
//
// ASORT RESULT
// $res1 = array(
// [b] => TRACE
// [c] => TeleMatch
// [a] => Trans Union
// [d] => test);
//
// DESIRED RESULT
// $res2 = array(
// [c] => TeleMatch
// [d] => test
// [b] => TRACE
// [a] => Trans Union);
function asorti($arr) {
$arr2 = $arr;
foreach($arr2 as $key => $val) {
$arr2[$key] = strtolower($val);
}
asort($arr2);
foreach($arr2 as $key => $val) {
$arr2[$key] = $arr[$key];
}
return $arr2;
}
http://php.net/manual/en/function.asort.php