note 100628 added to language.types.boolean
| From: | ledadu at gmail dot com | Date: | Wed, 27 Oct 2010 15:56:20 +0000 |
| Subject: | note 100628 added to language.types.boolean | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-173507@lists.php.net to get a copy of this message | ||
Function to sort array by elements and count of element (before php 5.3) (not use Lambda Functions,
and Closures)
<?php
//-----------------------------
function arraySortByElements($array2sort,$sortField,$order,$iscount=false) {
$functionString='
if ('.($iscount?'true':'false').'){
if(count($a["'.$sortField.'"]) >
count($b["'.$sortField.'"])) return 1*'.$order.';
if(count($a["'.$sortField.'"]) <
count($b["'.$sortField.'"])) return -1*'.$order.';
}else{
if($a["'.$sortField.'"] > $b["'.$sortField.'"])
return 1*'.$order.';
if($a["'.$sortField.'"] < $b["'.$sortField.'"])
return -1*'.$order.';
}
return 0;';
usort($array2sort, create_function('$a,$b',$functionString));
return $array2sort;
}
//-----------------------------
//init Array for testing :
$testArray = array(
array('name' => 'Lenny', 'note' => 5, 'listId'
=> array(654,987,32165)),
array('name' => 'Olivier', 'note' =>3, 'listId'
=> array(2)),
array('name' => 'Gregory', 'note' => 1, 'listId'
=> array(45,58)),
array('name' => 'Clement', 'note' => 2, 'listId'
=> array(584,587,741,14781,147))
);
//sorted Arrays :
$testArrayByNameASC = arraySortByElements($testArray,'name',1);
$testArrayByNoteDESC = arraySortByElements($testArray,'note',-1);
$testArrayByCountlistIdDESC = arraySortByElements($testArray,'listId',-1,true);
?>
----
Server IP: 69.147.83.197
Probable Submitter: 89.85.249.220
----
Manual Page -- http://www.php.net/manual/en/language.types.boolean.php
Edit -- https://master.php.net/note/edit/100628
Del: integrated -- https://master.php.net/note/delete/100628/integrated
Del: useless -- https://master.php.net/note/delete/100628/useless
Del: bad code -- https://master.php.net/note/delete/100628/bad+code
Del: spam -- https://master.php.net/note/delete/100628/spam
Del: non-english -- https://master.php.net/note/delete/100628/non-english
Del: in docs -- https://master.php.net/note/delete/100628/in+docs
Del: other reasons-- https://master.php.net/note/delete/100628
Reject -- https://master.php.net/note/reject/100628
Search -- https://master.php.net/manage/user-notes.php