I have an associative array of this kind:
array (size=3)
0 =>
array (size=2)
'percent' => float 70.12
'txt' => string 'test' (length=4)
1 =>
array (size=2)
'percent' => float 64.97
'txt' => string 'test' (length=4)
2 =>
array (size=2)
'percent' => float 89.78
'txt' => string 'test' (length=4)
and i need to sort it by the percent
field.
Example:
array (size=3)
0 =>
array (size=2)
'percent' => float 89.78
'txt' => string 'test' (length=4)
1 =>
array (size=2)
'percent' => float 70.12
'txt' => string 'test' (length=4)
2 =>
array (size=2)
'percent' => float 64.97
'txt' => string 'test' (length=4)
I saw there are several PHP function to order arrays but i can't find the proper one!
if $array is your array
usort($array, function($a, $b) {
return $b['percent'] - $a['percent'];
});
print_r($array); //to check it worked
You can use the array_multisort
, so you can sort by another fields too.
foreach ($data as $key => $row) {
$percent[$key] = $row['percent'];
$txt[$key] = $row['txt'];
}
array_multisort($percent, SORT_DESC, $txt, SORT_ASC, $data);
try like this, assuming that your array name is $array
usort($array, function ($a, $b) {
return $a['percent'] < $b['percent'];
});
print_r($array);
$array=array(array ('percent' => 70.12,'txt' =>'test'),array ('percent' => 64.97,'txt' =>'test'),array ('percent' => 89.78,'txt' =>'test'));
array_multisort($array, SORT_DESC);
print_r($array);
//output Array ( [0] => Array ( [percent] => 89.78 [txt] => test ) [1] => Array ( [percent] => 70.12 [txt] => test ) [2] => Array ( [percent] => 64.97 [txt] => test ) )
Try this (assuming $list
is your array):
usort($list,
function(array $a, array $b) {
if ($a['percent'] < $b['percent']) {
return -1;
} elseif ($a['percent'] == $b['percent']) {
return 0;
} else {
return +1;
}
}
)
Or swap -1
and +1
to sort descending.
/**
* Sort a 2 dimensional array based on 1 or more indexes.
*
* msort() can be used to sort a rowset like array on one or more
* 'headers' (keys in the 2th array).
*
* @param array $array The array to sort.
* @param string|array $key The index(es) to sort the array on.
* @param int $sort_flags The optional parameter to modify the sorting
* behavior. This parameter does not work when
* supplying an array in the $key parameter.
*
* @return array The sorted array.
*/
function msort($array, $key, $sort_flags = SORT_REGULAR) {
if (is_array($array) && count($array) > 0) {
if (!empty($key)) {
$mapping = array();
foreach ($array as $k => $v) {
$sort_key = '';
if (!is_array($key)) {
$sort_key = $v[$key];
} else {
// @TODO This should be fixed, now it will be sorted as string
foreach ($key as $key_key) {
$sort_key .= $v[$key_key];
}
$sort_flags = SORT_STRING;
}
$mapping[$k] = $sort_key;
}
asort($mapping, $sort_flags);
$sorted = array();
foreach ($mapping as $k => $v) {
$sorted[] = $array[$k];
}
return $sorted;
}
}
return $array;
}
Sample Input:
$tickets = array(
array(
'id' => 13,
'owner' => 'jachim',
'time' => '2009-09-25 10:39:42.011612',
'project' => 'jachim.be',
'title' => 'Some random ticket'
),
array(
'id' => 31,
'owner' => 'jachim',
'time' => '2009-09-24 14:38:47.945020',
'project' => 'joggink.be',
'title' => 'Some other random ticket'
),
array(
'id' => 22,
'owner' => 'root',
'time' => '2009-09-24 10:58:02.904198',
'project' => 'joggink.be',
'title' => 'A specific ticket'
)
);
Function call: msort($tickets, array('id')); //This will sort $tickets on id column.
Sample Output:
Array
(
[0] => Array
(
[id] => 13
[owner] => jachim
[time] => 2009-09-25 10:39:42.011612
[project] => jachim.be
[title] => Some random ticket
)
[1] => Array
(
[id] => 22
[owner] => root
[time] => 2009-09-24 10:58:02.904198
[project] => joggink.be
[title] => A specific ticket
)
[2] => Array
(
[id] => 31
[owner] => jachim
[time] => 2009-09-24 14:38:47.945020
[project] => joggink.be
[title] => Some other random ticket
)
)
Reference:
http://blog.jachim.be/2009/09/php-msort-multidimensional-array-sort/comment-page-1/
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.