So I have array with Passive, Q, Q2, W, W2, E, E2, R, R2 and I'd like to be in the order I wrote it in.
$SpellTitle = array("Passive", "W", "Q", "Q2", "W2", "R", "E", "E2", "R2");
I've read about using usort(), but I don't really understand how to use it.
UPDATE!
$SpellTitle is a dynamic array loaded from database so length will differ and it will be randomly set in that array.
usort
takes a user defined function as a second parameter. That function has to return an integer value of less than zero, zero, or greater than zero. This function has to take two parameters - array values to be compared to each other. If the first value is "lower" than the second, function returns a value less than zero, otherwise greater then zero. If values are equal, function must return zero.
So, If You want to make a custom function that sotrs integer or float values, You could use:
function cmp($a, $b) {
if((float) $a == (float) $b) {
return 0;
} else {
return ((float) $a < (float) $b) ? -1 : 1;
}
}
Use PHP asort
asort - Sort an array and maintain index association.
Example:
$SpellTitle = array("Passive", "W", "Q", "Q2", "W2", "R", "E", "E2", "R2");
asort($SpellTitle);
print_r($SpellTitle);
output:
Array
(
[6] => E
[7] => E2
[0] => Passive
[2] => Q
[3] => Q2
[5] => R
[8] => R2
[1] => W
[4] => W2
)
Note that array indices order was maintained too.
Hope this help.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.