sorting an array [SOLVED]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

raghavan20 wrote:

Code: Select all

$trans = array("a" => 1, "b" => 1, "c" => 2);
$trans = array_flip($trans);//flip keys and values
asort($trans);//sort based on keys now
$trans = array_flip($trans);//flip back again
print_r($trans);
Doesn't that do the opposite of what he required!?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Pimptastic wrote:
raghavan20 wrote:

Code: Select all

$trans = array("a" => 1, "b" => 1, "c" => 2);
$trans = array_flip($trans);//flip keys and values
asort($trans);//sort based on keys now
$trans = array_flip($trans);//flip back again
print_r($trans);
Doesn't that do the opposite of what he required!?
Indeed! But useful knowledge. I shall remember that when the situation arises. :lol:
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

scrotaye wrote:
Pimptastic wrote:
raghavan20 wrote:

Code: Select all

$trans = array("a" => 1, "b" => 1, "c" => 2);
$trans = array_flip($trans);//flip keys and values
asort($trans);//sort based on keys now
$trans = array_flip($trans);//flip back again
print_r($trans);
Doesn't that do the opposite of what he required!?
Indeed! But useful knowledge. I shall remember that when the situation arises. :lol:
I thought you wanted to sort an array by values.
if yes,
i exchange values and keys first
sort by asc
i exchange keys and values again :)
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Actually, I do believe you are right. That code would do what I required. I haven't tested it out though.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Code: Select all

$trans = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");

$trans = array_flip($trans);//flip keys and values 
asort($trans);//sort based on keys now 
$trans = array_flip($trans);//flip back again 
print_r($trans); 

// Output: Array ( [a] => orange [b] => banana [c] => apple [d] => lemon ) 

$trans = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
asort($trans);
print_r($trans); 

// Array ( [c] => apple [b] => banana [d] => lemon [a] => orange )
Post Reply