note 83117 added to function.array-multisort
| From: | php{dot}net | Date: | Sun, 11 May 2008 09:16:46 +0000 |
| Subject: | note 83117 added to function.array-multisort | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-139264@lists.php.net to get a copy of this message | ||
I was requiring a PHP function a sort my array data as part of an SQL interpreter for PHP arrays.
This is the code I came up with. and works wonderfully.
I hope this helps somebody. If anyone uses this, let me know what you think, if there are any
problems with it...
<?
## ##
## PHPMultiSort ##
## ##
// Takes:
// $data, multidim array
// $keys, array(array(key=>col1, sort=>desc), array(key=>col2, type=>numeric))
function php_multisort($data,$keys){
// List As Columns
foreach ($data as $key => $row) {
foreach ($keys as $k){
$cols[$k['key']][$key] = $row[$k['key']];
}
}
// List original keys
$idkeys=array_keys($data);
// Sort Expression
$i=0;
foreach ($keys as $k){
if($i>0){$sort.=',';}
$sort.='$cols['.$k['key'].']';
if($k['sort']){$sort.=',SORT_'.strtoupper($k['sort']);}
if($k['type']){$sort.=',SORT_'.strtoupper($k['type']);}
$i++;
}
$sort.=',$idkeys';
// Sort Funct
$sort='array_multisort('.$sort.');';
eval($sort);
// Rebuild Full Array
foreach($idkeys as $idkey){
$result[$idkey]=$data[$idkey];
}
return $results;
}
###############
// Example Data
$_DATA['table1'][] = array("name" => "Sebastian", "age"
=> 18, "male" => true);
$_DATA['table1'][] = array("name" => "Lawrence", "age"
=> 16, "male" => true);
$_DATA['table1'][] = array("name" => "Olivia", "age"
=> 10, "male" => false);
$_DATA['table1'][] = array("name" => "Dad", "age"
=> 50, "male" => true);
$_DATA['table1'][] = array("name" => "Mum", "age"
=> 40, "male" => false);
$_DATA['table1'][] = array("name" => "Sebastian", "age"
=> 56, "male" => true);
$_DATA['table1'][] = array("name" => "Lawrence", "age"
=> 19, "male" => true);
$_DATA['table1'][] = array("name" => "Olivia", "age"
=> 24, "male" => false);
$_DATA['table1'][] = array("name" => "Dad", "age"
=> 10, "male" => true);
$_DATA['table1'][] = array("name" => "Mum", "age"
=> 70, "male" => false);
###############
$res=php_multisort($_DATA['table1'],
array(array('key'=>'name'),array('key'=>'age','sort'=>'desc')))
var_dump($res);
/*
array(10) {
[8]=>
array(3) {
["name"]=>
string(3) "Dad"
["age"]=>
int(10)
["male"]=>
bool(true)
}
[3]=>
array(3) {
["name"]=>
string(3) "Dad"
["age"]=>
int(50)
["male"]=>
bool(true)
}
[1]=>
array(3) {
["name"]=>
string(8) "Lawrence"
["age"]=>
int(16)
["male"]=>
bool(true)
}
[6]=>
array(3) {
["name"]=>
string(8...
*/
?>
----
Server IP: 92.48.74.199
Probable Submitter: 86.141.12.93
----
Manual Page -- http://www.php.net/manual/en/function.array-multisort.php
Edit -- https://master.php.net/note/edit/83117
Del: integrated -- https://master.php.net/note/delete/83117/integrated
Del: useless -- https://master.php.net/note/delete/83117/useless
Del: bad code -- https://master.php.net/note/delete/83117/bad+code
Del: spam -- https://master.php.net/note/delete/83117/spam
Del: non-english -- https://master.php.net/note/delete/83117/non-english
Del: in docs -- https://master.php.net/note/delete/83117/in+docs
Del: other reasons-- https://master.php.net/note/delete/83117
Reject -- https://master.php.net/note/reject/83117
Search -- https://master.php.net/manage/user-notes.php