Page 2 of 2
Posted: Wed Aug 24, 2005 5:17 am
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!?
Posted: Wed Aug 24, 2005 5:18 am
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.

Posted: Wed Aug 24, 2005 5:20 am
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.

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

Posted: Wed Aug 24, 2005 5:22 am
by s.dot
Actually, I do believe you are right. That code would do what I required. I haven't tested it out though.
Posted: Wed Aug 24, 2005 5:31 am
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 )