note 88193 added to function.array-multisort
| From: | frank at overdrunk dot net | Date: | Wed, 14 Jan 2009 22:38:16 +0000 |
| Subject: | note 88193 added to function.array-multisort | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-149204@lists.php.net to get a copy of this message | ||
I had a function to make a sort on a 2D array and I wanted to sort an array using a column that
usualy contains numeric values but also strings.
Lets say we have this array :
Array (
[0] => Array ( "name" = "12000" ),
[1] => Array ( "name" = "113" ),
[2] => Array ( "name" = "test 01" ),
[3] => Array ( "name" = "15000 tests" ),
[4] => Array ( "name" = "45" ),
[5] => Array ( "name" = "350" ),
[6] => Array ( "name" = "725" ),
[7] => Array ( "name" = "hello" )
}
SORT_STRING whould have returned me this :
Array ( // Numeric values are not correctly sorted
[0] => Array ( "name" = "113" ),
[1] => Array ( "name" = "12000" ),
[2] => Array ( "name" = "15000 tests" ),
[3] => Array ( "name" = "350" ),
[4] => Array ( "name" = "45" ),
[5] => Array ( "name" = "725" ),
[6] => Array ( "name" = "hello" ),
[7] => Array ( "name" = "test 01" )
}
SORT_NUMERIC would have returned me this :
Array ( // String values are not sorted, just in the same order
[0] => Array ( "name" = "test 01" ),
[1] => Array ( "name" = "hello" ),
[2] => Array ( "name" = "45" ),
[3] => Array ( "name" = "113" ),
[4] => Array ( "name" = "350" ),
[5] => Array ( "name" = "725" ),
[6] => Array ( "name" = "12000" ),
[7] => Array ( "name" = "15000 tests" ),
}
So I've made this hybrid code which combines the best of both worlds by merging content sorted
either way according to the first caracter of the string:
<?php
/**
* Sorts an array according to a specified column
* Params : array $table
* string $colname
* bool $numeric
**/
function sort_col($table, $colname) {
$tn = $ts = $temp_num = $temp_str = array();
foreach ($table as $key => $row) {
if(is_numeric(substr($row[$colname], 0, 1))) {
$tn[$key] = $row[$colname];
$temp_num[$key] = $row;
}
else {
$ts[$key] = $row[$colname];
$temp_str[$key] = $row;
}
}
unset($table);
array_multisort($tn, SORT_ASC, SORT_NUMERIC, $temp_num);
array_multisort($ts, SORT_ASC, SORT_STRING, $temp_str);
return array_merge($temp_num, $temp_str);
}
?>
It would return something like this :
Array (
[2] => Array ( "name" = "45" ),
[3] => Array ( "name" = "113" ),
[4] => Array ( "name" = "350" ),
[5] => Array ( "name" = "725" ),
[6] => Array ( "name" = "12000" ),
[7] => Array ( "name" = "15000 tests" ),
[1] => Array ( "name" = "hello" ),
[0] => Array ( "name" = "test 01" ),
}
----
Server IP: 66.207.199.35
Probable Submitter: 216.137.100.63
----
Manual Page -- http://www.php.net/manual/en/function.array-multisort.php
Edit -- https://master.php.net/note/edit/88193
Del: integrated -- https://master.php.net/note/delete/88193/integrated
Del: useless -- https://master.php.net/note/delete/88193/useless
Del: bad code -- https://master.php.net/note/delete/88193/bad+code
Del: spam -- https://master.php.net/note/delete/88193/spam
Del: non-english -- https://master.php.net/note/delete/88193/non-english
Del: in docs -- https://master.php.net/note/delete/88193/in+docs
Del: other reasons-- https://master.php.net/note/delete/88193
Reject -- https://master.php.net/note/reject/88193
Search -- https://master.php.net/manage/user-notes.php