I have this array and I want to sort it (ASC)?
$stand_array[$player_name][$player_points] = $player_rank;
print_r
Array
(
[Player1] => Array
(
[50] => 5.7
)
[Player2] => Array
(
[40] => 4.2
)
[Player3] => Array
(
[30] => 3.7
)
[Player4] => Array
(
[20] => 2.3
)
[Player5] => Array
(
[10 => 1.5
)
[Player6] => Array
(
[60] => 6.3
)
)
Would you like to help me to solve this array on $player_rank (ASC)?
NB: I tried this function, but it did not work:
function sortByOrder($a, $b) {
return $a[$player_rank] - $b[$player_rank];
}
usort($myArray, 'sortByOrder');
The variable $player_rank is not visible from the sortByOrder function scope. Also, the indexes is different in each player array, so you need to access it like this:
function sortByOrder($a, $b)
{
$a = end($a);
$b = end($b);
if ($a == $b)
{
return 0;
}
return ($a < $b) ? -1 : 1;
}
usort($myArray, 'sortByOrder');
And if you want to save keys in $myArray then you must use the uasort function instead of usort.
Related
I have to sort below given array by Priority. If priority value is 0 then not include in sorting array. I have tried many ways but not getting output as expected.
Array
(
[recently_viewed] => Array
(
[priority] => 1
[no_of_products] => 1
)
[recently_visited_cat] => Array
(
[priority] => 1
[no_of_products] => 1
)
[last_ordered_items] => Array
(
[priority] => 3
[no_of_products] => 3
)
[searched_based] => Array
(
[priority] => 0
[no_of_products] => 4
)
[cart_based] => Array
(
[priority] => 1
[no_of_products] => 5
)
[wishlist_based] => Array
(
[priority] => 1
[no_of_products] => 6
)
[sku_based] => Array
(
[priority] => 0
[no_of_products] => 7
)
)
Please help me with this.
You can use array_filter() function to exclude records with 0 priority. After it you can sort the array with help uasort() function. For example:
$arr = array_filter($arr, function($element) {
return $element['priority'] > 0;
});
uasort($arr, function ($a, $b) {
if ($a['priority'] === $b['priority']) {
return 0;
}
return ($a['priority'] < $b['priority']) ? -1 : 1;
});
You can us array_filter (doc - for check bigger then 0) and then uasort (doc):
$arr = [];
$arr["recently_viewed"] = ["priority" => 1, "no_of_products" =>1];
$arr["searched_based"] = ["priority" => 0, "no_of_products" =>4];
$arr["last_ordered_items"] = ["priority" => 3, "no_of_products" =>3];
$arr = array_filter($arr, function ($e) {return $e["priority"];});
uasort($arr, function($a, $b) {
return $a['priority'] - $b['priority'];
});
Live example: 3v4l
You can use array_filter with callback function to remove the values with ZERO
$arr = array_filter($arr, function ($e) {return $e["priority"];});
This will remove all the sub-array which has priority values ZERO.
Now you can apply usort to sort the array
usort($arr, function($a, $b) {
return $a['priority'] <=> $b['priority'];
});
See Working Code Live
Today I provided the answer of this question and I wrote a script, but I found out that something went wrong.
Here is the first script
<?php
$array = array(
"0" => array (
"id" => 1204,
"custom_price" => 33.1500
),
"1" => array (
"id" => 1199,
"custom_price" => 15.83
),
"2" => array (
"id" => 1176,
"custom_price" => 16.83
)
);
usort($array, function($a, $b) {
return $a['custom_price'] - $b['custom_price'];
});
echo "<pre>";
print_r($array);
and its output is (also you can check output on sandbox)
<pre>Array
(
[0] => Array
(
[id] => 1176
[custom_price] => 16.83
)
[1] => Array
(
[id] => 1199
[custom_price] => 15.83
)
[2] => Array
(
[id] => 1204
[custom_price] => 33.15
)
)
So, my desired output should be sort like (custom_price 15.83, 16.83, 33.15000) but the actual output is (custom_price 16.83,15.83,33.15000). you can see 15.83 is smallest from 16.83. the sorting result is wrong
So, when I change custom_price 15.83 to 14.83 then sorting output is correct
<pre>Array
(
[0] => Array
(
[id] => 1199
[custom_price] => 14.83
)
[1] => Array
(
[id] => 1176
[custom_price] => 16.83
)
[2] => Array
(
[id] => 1204
[custom_price] => 33.15
)
)
you can see output on sandbox
I can't understand what's going on.. any idea about this ?
My Question is: I check each iteration but can't identify the problem. when custom_price is 15.83 then result is wrong. why?
There is a warning in the PHP manual about the return values from the usort() compare function (at http://php.net/manual/en/function.usort.php#refsect1-function.usort-parameters)...
Caution Returning non-integer values from the comparison function,
such as float, will result in an internal cast to integer of the
callback's return value. So values such as 0.99 and 0.1 will both be
cast to an integer value of 0, which will compare such values as
equal.
Also from PHP 7. you can use the spaceship operator <=> which returns 1, 0, -1 depending on the comparison of the two values...
usort($array, function($a, $b) {
return $a['custom_price'] <=> $b['custom_price'];
});
echo "<pre>";
print_r($array);
There is a complete example in the PHP manual about usort. Here's the modified version to solve your problem:
<?php
function cmp($a, $b)
{
if ($a['custom_price'] == $b['custom_price']) {
return 0;
}
return ($a['custom_price'] < $b['custom_price']) ? -1 : 1;
}
Below code will solve your problem,
usort($array, function($a, $b) {
if($a['custom_price']==$b['custom_price']) return 0;
return $a['custom_price'] > $b['custom_price'] ? 1 : -1;
});
Updated function
usort($array, function($a, $b) {
return $a['custom_price'] > $b['custom_price'];
});
Hey guys I'm trying to reverse sort a multidimensional array with usort but am messing up somewhere. Here's my code:
$array = array(
array(123 => 'foo'), // duplicate
array(124 => 'foo'),
array(127 => 'foo'),
array(126 => 'foo'),
array(123 => 'foo'), // duplicate
array(125 => 'foo'),
);
function rcmp($a, $b) {
if($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}
usort($array, 'rcmp');
print_r($array);
/*
Array
(
[0] => Array ( [126] => foo) )
[1] => Array ( [125] => foo) )
[2] => Array ( [127] => foo) )
[3] => Array ( [123] => foo) )
[4] => Array ( [124] => foo) )
[5] => Array ( [123] => foo) )
)
*/
I am expecting
/*
Array
(
[0] => Array ( [127] => foo) )
[1] => Array ( [126] => foo) )
[2] => Array ( [125] => foo) )
[3] => Array ( [124] => foo) )
[4] => Array ( [123] => foo) )
[5] => Array ( [123] => foo) )
)
*/
What am I doing wrong?
If you want to compare on the array's indexes, you must extract the indexes in the comparison function and reverse the comparison for descending order
function rcmp($a, $b) {
$a = array_keys($a);
$b = array_keys($b);
if($a[0] == $b[0]) {
return 0;
}
return ($a[0] < $b[0]) ? 1 : -1;
}
When your comparison function rcmp is invoked, its arguments $a and $b are arrays so your equality and less-than operators have arrays as their operands.
The equality operator on arrays works like this:
TRUE if $a and $b have the same key/value pairs.
The less-than comparison works like this:
Array with fewer members is smaller, if key from operand 1 is not found in operand 2 then
arrays are uncomparable, otherwise - compare value by value.
"Uncomparable" means the comparison evaluates to null, which in turn causes rcmp to return 1.
So what ends up happening is that when $a and $b are not identical arrays rcmp always returns 1 (i.e. considers that $a is greater). This happens irrespective of what the key in each array is, and as a result you get a meaningless ordering.
If you wanted to order these arrays by their first key with usort, you would do it like this:
function rcmp($a, $b)
{
return key($a) - key($b);
}
Here's a solution using create_function that I came up with, accepting the best answer though. Thanks and hope this helps someone else out.
$array = array(
array(124 => 'foo'),
array(123 => 'foo'), // duplicate
array(127 => 'foo'),
array(126 => 'foo'),
array(123 => 'foo'), // duplicate
array(125 => 'foo'),
);
usort($array, create_function('$a, $b','return (key($a) == key($b))
? 0
: (key($a) < key($b))
? -1
: 1;'));
print_r($array);
I'm having a problem. I have a multidimensional array, that looks like this:
Array ( [0] =>
Array (
[0] => Testguy2's post.
[1] => testguy2
[2] => 2013-04-03
)
[1] => Array (
[0] => Testguy's post.
[1] => testguy
[2] => 2013-04-07
)
);
I want to sort the posts from the newest date to the oldest date, so it looks like this:
Array ( [1] => Array (
[0] => Testguy's post.
[1] => testguy
[2] => 2013-04-07
)
[0] => Array (
[0] => Testguy2's post.
[1] => testguy2
[2] => 2013-04-03
)
);
How do I sort it?
function cmp($a, $b){
$a = strtotime($a[2]);
$b = strtotime($b[2]);
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}
usort($array, "cmp");
Or for >= PHP 7
usort($array, function($a, $b){
return strtotime($a[2]) <=> strtotime($b[2]);
});
You can do it using usort with a Closure :
usort($array, function($a, $b) {
$a = strtotime($a[2]);
$b = strtotime($b[2]);
return (($a == $b) ? (0) : (($a > $b) ? (1) : (-1)));
});
I'm just stepping away from my desk for the day so I can't offer specifics. But here's a good place to get started that includes examples: array_multisort
$dates = array();
foreach($a AS $val){
$dates[] = strtotime($val[2]);
}
array_multisort($dates, SORT_ASC, $a);
Can anyone tell me how to sort an array by key?
I want to sort this array by price.
this is my input array format
Array
(
[0] => Array
(
[house_data] => Array
(
[id] => 532
[max_person] => 8
[max_bedrooms] => 4
)
[image] => uploads/123.jpg
[price] => 1950
)
[1] => Array
(
[house_data] => Array
(
[id] => 531
[max_person] => 8
[max_bedrooms] => 5
)
[image] => uploads/1234.jpg
[price] => 1495
)
}
Try usort (http://php.net/manual/en/function.usort.php)
You should have something like:
function cmp($a, $b)
{
if ($a['price'] == $b['price']) {
return 0;
}
return ($a['price'] < $b['price']) ? -1 : 1;
}
usort($table, "cmp");
For making it one dimensional use serialize($array) function , it goes like this :$a = array() ; //your multidimensional array$b = array(); //output arrayforeach ($a as $key=>$value){ $b[] = serialize($value);}echo $b ;
Use the array_multisort() function for multidimensional array sorting.