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);
}
...
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 a simple question:
Query returns array in which I would like to change order of elements in PHP.
I have an array like this:
$typesSumAr = array( 'break', 'private absence', 'sick leave', 'vacation', 'work', 'work absence' );
I would like to have an array in this order:
$typesSumAr = array( 'work', 'break', 'sick leave', 'vacation', 'private absence', 'work absence' );
The are not always all elements in array, it could be only two for example, so I cannot hardcode the array. Do I need to make if statemenets to find out if key exists and then order it manually?
Thank you for your answer.
Seeing as you have an array in the order you prefer, your problem boils down to keeping the elements that are also present in another array. PHP has a function for exactly that: array_intersect
array_intersect returns an array containing all the values of its first array argument that are present in all the arguments. Note that keys are preserved.
There are lots of great array sorting function depending on how you want to sort it. Have a look here http://www.php.net/manual/en/array.sorting.php or even based on your own function via uksort (http://www.php.net/manual/en/function.uksort.php)
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.
I am adding elements to multidimensional array like this
foreach($results as $res){
$fwidth=$res[0];
$fpath=$res[1];
$sub = array (
'img_w' => $fwidth,
'img_path' => $fpath,
);
$widths[] = $sub;
}
And i want to sort $widths array by 'img_w' from bigger to smaller (DESC).
Can anyone help me? Thanks in advance.
Use PHP's USort() function (user sort), which takes two arguments. The first is the array you want to sort. The second is a function that does the actual sorting (you write it yourself). The page I linked to has some examples that should cover your case.