how to change array element position PHP? - php

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

Related

PHP- Sort Indexed array according to the values of another 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

PHP sort objects value but retain original relative order [duplicate]

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

Display an array 'in order'?

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.

JSON get highest to lowest value data multi array

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

Sorting multidimensional array

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.

Categories