PHP: Sorting a multidimensional array by an attribute - php

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']);
});

Related

Sort array by key value and then get 5 highest and lowest values in array

I have an array called "playersArray" that looks like this:
Array (
[0] => Array ( [name] => Joe [holders] => 0 )
[1] => Array ( [name] => Bob [holders] => 100 )
[2] => Array ( [name] => Jake [holders] => 100 )
[3] => Array ( [name] => Mike [holders] => 100 )
[4] => Array ( [name] => Tim [holders] => -0.0145 )
[5] => Array ( [name] => Frank[holders] => 100 )
[6] => Array ( [name] => Scott [holders] => 0.0583 )
[7] => Array ( [name] => Doug[holders] => 0.1308 )
[8] => Array ( [name] => Tommy [holders] => 0.2516 )
[9] => Array ( [name] => Eric [holders] => 100 )
)
I have a function to sort this array by the "holders" value:
function compareHolders($a, $b) {
$aPoints < $a['holders'];
$bPoints < $b['holders'];
return strcmp($aPoints, $bPoints);
}
I loop through another array to create this array:
foreach ($players as $player) {
$player['name'] = $athlete['name'];
$player['holders'] = $total_result_yesterday;
$playersArray[] = $player;
}
I am trying to sort the array by "holder" value:
usort($playersArray, 'compareHolders');
print_r($playersArray);
Finally, I am trying to get the 5 highest and 5 lowest "holder" values in the newly sorted array:
$first_5_players = array_slice($playersArray, 0, 5, true);
$last_5_players = array_slice($playersArray, -5);
However, the sorting is not working correctly. The values due not show in sequential order as desired. How can I get the sorting to work correctly? Thank you!
your sorting function compareHolders is not correct. $aPoints and $bPoints are not defined. Since values for holders key are numeric you can use the comparision operators. Try doing following:
function compareHolders($a, $b) {
if ($a['holders'] == $b['holders']) {
// return 0 if equal
return 0;
}
return ($a['holders'] > $b['holders']) ? -1 : 1;
}
You are not actually comparing the two values in compareHolders(), because you're not declaring $aPpoints and $bPoints.
This should work:
function compareHolders($a, $b) {
$aPoints = $a['holders'];
$bPoints = $b['holders'];
return strcmp($aPoints, $bPoints);
}
or alternatively you could just return:
return strcmp($a['holders'], $b['holders']);
And then you could get rid of the strcmp(), since you're not comparing strings.

how can I get sorting multidimnsion array by value?

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

Sorting an array according to a key in multi dimensional array and preserving other key values. PHP

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");

PHP array sort using inner val

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'];
}

sort an array base on key

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;
}

Categories