I've got an array called $rank_array:
Array ( [Tribus Bella] => 179 ) Array ( [TestClan] => 767 )
When I run this code:
foreach ($rank_array as $clan => $rank) {
echo $clan.' = '.$rank.'<br />';
}
I get the following:
Tribus Bella = 179
TestClan = 767
I'd like to display it in the reverse order (so the it's ordered by the $rank variable), but when I use something like asort it doesn't change the order at all.
Can anyone help explain why? And help me fix it?
edit
None of the functions seem to be working (arsort, asort, etc) so I'm wondering if it's the way I'm inserting the data into the array.
I'm inserting it with this code
$rank_array = array($q['name'] => $clan_total_points);
Is that wrong?
The default sort flag for asort() is SORT_REGULAR, which will sort in ascending order - which the order in which they already are. You need to sort into descending order, which you would do like this:
asort($rank_array, SORT_DESC);
Now when you loop $rank_array, it will be in the order in which you want it. Wrong!
As #Nameless has correctly pointed out, the correct answer to this question is that you need to use arsort() in order to achieve what you want.
For more-to-less value sorting use arsort function.
Related
hey folks I need to sort an array by an specific order, how can I do that?
Original array:
array ('despacho','noc1','suporte','triagem','validacao');
Wanted order:
array ('validacao','triagem','noc1','despacho','suporte');
It's not by ASC or DESC, it's and specific order.
hope you guys could help me. tks..
I don't know why you would do this when you can just define the array in the order that you want. But if you define an order and compute it's intersection with the original they will return in that order:
$array = array ('despacho','noc1','suporte','triagem','validacao');
$order = array ('validacao','triagem','noc1','despacho','suporte');
$array = array_intersect($order, $array);
Basically,I want to sort an array based on another array.
As in,
<?php
$movies=array("Avengers","Spiderman","Batman","Flash");
$ratings=array(9,3,4,7,);
?>
In other words,the $movies[0]("Avengers") has a ratings of 9 , $movies[1]("Spiderman") has a rating of 3 and so on.
I want the ratings array to be sorted numerically in descending order and thus sorting the movies array.I will like this to be in a function.
Thus, the array will be thus:
<?php
$movies=array("Spiderman","Batman","Flash","Avengers",);
$ratings=array(3,4,7,9);
?>
The ratings array has been sorted and thus the movies array has also been sorted but according to the ratings array.
I am sorry I am a newbie in php and I know that doing that is probably very simple but I don't know how to do It.The only way I can think of is using inserting into an sql table then selecting from it in descending order but that will probably be a very bad idea.....
Any help is highly appreciated. Thanks in advance.
Oh and if it is easier to do it in Laravel (I use laravel) please show me how
You can use array_combine() to combine movie names and ratings.
So, that movie names will be the keys of resulting array and ratings will be the values.
Now, sort using arsort() so that the new array will be sorted in descending order by ratings keeping movie names as it is.
Print the array.
Code:
<?php
$movies=array("Spiderman","Batman","Flash","Avengers",);
$ratings=array(3,4,7,9);
$movieRatings = array_combine($movies, $ratings);
arsort($movieRatings);
echo '<pre>';print_r($movieRatings);
?>
Output:
Array
(
[Avengers] => 9
[Flash] => 7
[Batman] => 4
[Spiderman] => 3
)
try this,
array_combine ( array $keys , array $values ) : array
it works by setting the values in one array as keys of another array which in your case is your movies.
<?php
$movies=array("Avengers","Spiderman","Batman","Flash");
$ratings=array(9,3,4,7,);
$result = array_combine ( $movies, $ratings);
// then sort this way...
arsort($result, 0);
?>
documentation for both functions are here...
https://www.php.net/manual/en/function.array-combine.php
https://www.w3schools.com/php/func_array_arsort.asp
I have an array as follows:
$aq=['jonathan','paul','andy','rachel'];
Then I have an array as follows:
$bq=['rachel','andy','jonathan'];
What I need is to use the ordering of the first array to sort my second array.
So for this instance, the resulting sorted array should be:
$cq=['jonathan','andy','rachel'];
I started working on a solution that uses the highest key as the top value (the head of the array) because what Im looking for is the top value but that ran into issues and seemed more like a hack so i think sorting is what im looking for.
Is there a simple function in php that can sort my data based on my first array and there respective positions in the array
please try this short and clean solution using array_intersect:
$aq = ['jonathan','paul','andy','rachel'];
$bq = ['rachel','andy','jonathan'];
$cq = array_intersect($aq, $bq);
var_export($cq);
the output will be :
array ( 0 => 'jonathan', 2 => 'andy', 3 => 'rachel', )
You'll have to use a custom sort function. Here we grab the keys of corresponding entries in the "ordering" array and use them to order the working array.
In this example, we give up (return 0) if the key doesn't exist in the ordering array; you may wish to customize that behavior, but this should give you the general idea.
$order = ['jonathan','paul','andy','rachel'];
$arrayToSort =['rachel','andy','jonathan'];
usort($arrayToSort,function($a,$b) use ($order){
if( ! array_key_exists($a,$order) ) return 0;
if( ! array_key_exists($b,$order) ) return 0;
if( array_search($a,$order) > array_search($b,$order)
return 1;
return -1;
});
This question already has answers here:
Preserve key order (stable sort) when sorting with PHP's uasort
(6 answers)
Closed 5 months ago.
I'm using usort to sort an array of objects, but really I want this to act as a kind of "group by" function without disturbing the original relative order of the rows.
Say I have this:
MASTER_CODE, CONFIG_ITEM
foo1, opt_ray
foo2, opt_ray
foo1, opt_fah
foo2, opt_doe
From that data, an array of objects is constructed with an anonymous key. That is, each row is parsed as an object. The objects are collected into an array.
What I want to do is sort the array by the MASTER_CODE value, but without disturbing the order.
That is, the final order should be:
MASTER_CODE, CONFIG_ITEM
foo1, opt_ray
foo1, opt_fah
foo2, opt_ray
foo2, opt_doe
We don't add a sort order, because the data comes from an external source.
I can use usort to order by the master code, but it messes up the original relative order.
Any suggestions?
This is one option - it's not the most elegant solution. It will take the unique values from the first column of your array (the one you want to filter by), sort that, then loop it and add entries from your original array with the same first value.
// Get an unique array of values to use for sorting
$sorting = array_unique(array_column($a, 0));
sort($sorting);
$sorted = [];
foreach ($sorting as $sortValue) {
$sorted = array_merge(
$sorted,
array_filter(
$a,
function($row) use ($sortValue) {
// Find values that have the same first value as each sort value
return ($sortValue === $row[0]);
}
)
);
}
Example
Note: This will work on PHP 5.5. Since you also tagged PHP 5.3, you may need to replace the array_column function. Try something like this:
$sorting = array_unique(array_map(function($row) { return $row[0]; }, $a));
Hello whew im stack in this problem.... arranging value from highest to lowest in json data values
pastebin - http://pastebin.com/yjFUfdJW for the codes
whew i just want to sort the likes highest to lowest... whew anyone has a solution for it? :) pleas help.. :)
Use one of PHP's sort functions for array sorting on the array-value of your JSON?
In order to sort your associative array and maintain the correlation between array elements, you need to use asort().
...
$like[] = array(
'uid' => $uid[$i],
'likes' => $datacount
);
foreach($like as $userlikes)
{
// Sort array numerically
asort($userlikes, SORT_NUMERIC);
}
...