First off I am new to this site and it is a big help, so thanks in advance for the input.
I am trying to shift a subset of array values after comparing them, like asort.
Here is what I have:
$array[name] = "name";
$array[date] = "date";
$array[item1] = 7;
$array[item2] = 16;
$array[item3] = 3;
$array[item4] = 16;
$array[item5] = 2;
$array[item6] = 10;
$array[author] = "author";
$array[location] = "location';
I would like to sort the itemsN values by sorting the values so the values of "16" are at end of the subset, and the values other than "16" are at the beginning of the subset.
So after the sort I want to end up with:
$array[name] = "name";
$array[date] = "date";
$array[item1] = 7;
$array[item2] = 3;
$array[item3] = 2;
$array[item4] = 10;
$array[item5] = 16;
$array[item6] = 16;
$array[author] = "author";
$array[location] = "location';
You mean you want to "sort an array in reverse order and maintain index association"?
arsort($array)
Or if you don't care about maintaing key/value association:
rsort($array)
check this reference
The ArrayObject has an asort()
method:
$array->asort();
foreach ($array as $key => $value) {
echo $key . ' - ' . $value . "\n";
}
Outputs:
1 - 2
2 - 2
3 - 1
4 - 1
5 - 1
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.