PHP How do I shift subset of array values to do something like asort?
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 as开发者_如何学JAVAort.
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';
check this reference
http://www.php.happycodings.com/code_snippets/code9.html
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)
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
精彩评论