views:

108

answers:

3

Hello. 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';
A: 

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)
R. Hill
He wants to sort an object, not an array ...
HoLyVieR
I see. I have to admit that calling his object "array" and wanting to sort its members got me confused here.
R. Hill
Sorry it is an array. Edited the question to reflect that. I want to shift the subset of values, not sort the entire array.
netefficacy
A: 

check this reference

http://www.php.happycodings.com/code_snippets/code9.html

CheeseConQueso
This did the trick, plus some other things. Thank you.
netefficacy
np - have a blast
CheeseConQueso
A: 

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
ircmaxell
yeah, but it will sort the keys as well
Gordon