How can I sorting multidimension array by value?
Example my output array like this :
[0] => Array
(
[0] => 2
[1] => 5
[2] => 8
[3] => 1
)
[1] => Array
(
[0] => 7
[1] => 4
[2] => 1
)
Can I get sorted array like this?
[0] => Array
(
[3] => 1
[0] => 2
[1] => 5
[2] => 8
)
[1] => Array
(
[2] => 1
[1] => 4
[0] => 7
)
Thanks..
simple way with asort
array_map(function($v){asort($v); return $v;}, $array);
Or you can use uasort()
You can use uasort and array_map to sort the elements. for php7+
array_map(function($v){uasort($v, function($a, $b){return $a <=> $b;}); return $v;}, $array);
for version<7 use this compare funciton of uasort
function cmp($a, $b) {
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}
You can use array_walk together with asort:
array_walk($array, 'asort');
Here is working demo.
You can use arsort to sort array by values. make a loop and call asort($arr) for each sub array you have
Related
I have an array like this one:
Array
(
[0] => Array
(
[id] => 1
[name] => Mickey
)
[1] => Array
(
[id] => 2
[name] => Donald
)
[2] => Array
(
[id] => 3
[name] => Goofy
)
)
Is there a way to sort in alphabetical order the 'name' field?
Yes, there is. Using a callback method for usort();
function my_sorter($a, $b) {
return strcmp($a['name'], $b['name']);
}
usort($list, 'my_sorter');
You could use usort() http://php.net/manual/en/function.usort.php
usort works fine. The function passed to usort should be a comparison function, which returns a value less than one if ab, and 0 if a==b. Because of how your arrays are formatted, your comparison function should compare a['name'] and b['name']. So:
function cmp($a, $b)
{
return strcmp($a['name'],$b['name']);
}
$a = array(array("id" => 2, "name" => "Donald"),array("id" => 3, "name" => "Goofy"),array("id" => 4, "name" => "Mickey"));
usort($a, "cmp");
foreach ($a as $key => $value) {
echo "$key: ".$value['name']."\n";
}
I've got a multi dimensional array as seen below. I want to sort the 2nd level arrays based the [date] attribute. I believe I can use array_multisort, but I'm unsure of how to proceed.
My array is in the variable $presentations
Array
(
[0] => Array
(
[date] => 20111104
[name] => Name of Presentation
)
[1] => Array
(
[date] => 20111118
[name] => sadf
)
[2] => Array
(
[date] => 20100427
[name] => older one
)
[3] => Array
(
[date] => 20101213
[name] => Another one from 2010
)
[4] => Array
(
[date] => 20110719
[name] => sdf
)
[5] => Array
(
[date] => 20110614
[name] => Sixth one
)
)
A usort callback should return 3 types of values, depending on the circumstances:
A negative number if parameter $a is less than $b
A positive number if parameter $b is less than $a
Zero if both $a and $b are equal
usort($presentations, function($a, $b)
{
if($a['date'] == $b['date'])
{
return 0;
}
return $a['date'] < $b['date'] ? -1 : 1;
});
You can use usort() to apply a custom comparison function.
usort($presentations,
function ($left, $right) {
return $left['date'] - $right['date'];
});
Here's a string implementation which works with integers in PHP because of type juggling:
usort($presentations, function($a, $b) {
return strcmp($a['date'], $b['date']);
});
This is my array.
Array
(
[Data] => Array
(
[0] => Array
(
[recipeid] => 108
[recipe] => Rasams- the tongue ticklers !
[image] => No data
[category] => Rasams and Soups
)
[1] => Array
(
[recipeid] => 44
[recipe] => Brain Booster- do you want to try it?
[image] => brain-booster-do-you-44-HP-62.jpg
[category] => Drinks and Smoothies
)
[2] => Array
(
[recipeid] => 36
[recipe] => Pineapple Grape Smoothy--a rare combo
[image] => pineapple-grape-smoo-36-HP-62.jpg
[category] => Drinks and Smoothies
)
)
)
I have to sort [DATA] array according to alphabetical order of [key]recipe's value, also preserve the recipeid, image, category after sorting.
Use usort.
usort($yourarray['Data'], 'data_sort');
function data_sort($a, $b) {
return (strcasecmp($a['recipe'], $b['recipe']) > 0);
}
You should be able to do this with usort(). Here's an example of how it could be done, based off of the example given in the PHP docs.
function cmp($a, $b)
{
if ($a['recipe'] == $b['recipe']) {
return 0;
}
return ($a['recipe'] < $b['recipe']) ? -1 : 1;
}
usort($a, "cmp");
Array
(
[1] => Array
(
[id] => 1
[sort] => 1
)
[3] => Array
(
[id] => 3
[sort] => 3
)
[2] => Array
(
[id] => 2
[sort] => 2
)
)
How do i sort it so its re-ordered using the inner 'sort' key ? ie the above would look like this:
Array
(
[1] => Array
(
[id] => 1
[sort] => 1
)
[2] => Array
(
[id] => 2
[sort] => 2
)
[3] => Array
(
[id] => 3
[sort] => 3
)
)
You can use usort with this comparison function:
function cmpBySort($a, $b) {
return $a['sort'] - $b['sort'];
}
usort($arr, 'cmpBySort');
Or you use array_multisort with an additional array of key values for the sort order:
$keys = array_map(function($val) { return $val['sort']; }, $arr);
array_multisort($keys, $arr);
Here array_map with the anonymous function is used to build an array of the sort values that is used to sort the array values itself. The advantage of this is that there is np comparison function that needs to be called for each pair of values.
Something like this:
usort($array, function (array $a, array $b) { return $a["sort"] - $b["sort"]; });
Something like this:
uasort($array, 'compfunc');
function compfunc($a, $b)
{
return $a['sort'] - $b['sort'];
}
i have an array like this:
Array
(
[0] => Array
(
[title] => some title
[time] => 1279231500
)
[1] => Array
(
[title] => some title 2
[time] => 1279231440
)
[2] => Array
(
[title] => some title 3
[time] => 1279229880
)
)
how i can sort it based on time?
You can sort it this way (since it is an associative array):
function cmp($a, $b)
{
return strcmp($a['time'], $b['time']);
}
usort($your_array, "cmp");
print_r($your_array);
As Gumbo mentioned, you should not use strcmp for integer values.
Use this function
function cmp($a, $b) {
if ($a['time'] == $b['time'])
return 0;
return ($a['time'] < $b['time']) ? -1 : 1;
}