Doesn't that do the opposite of what he required!?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);
sorting an array [SOLVED]
Moderator: General Moderators
Indeed! But useful knowledge. I shall remember that when the situation arises.Pimptastic wrote:Doesn't that do the opposite of what he required!?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);
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.
- raghavan20
- DevNet Resident
- Posts: 1451
- Joined: Sat Jun 11, 2005 6:57 am
- Location: London, UK
- Contact:
I thought you wanted to sort an array by values.scrotaye wrote:Indeed! But useful knowledge. I shall remember that when the situation arises.Pimptastic wrote:Doesn't that do the opposite of what he required!?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);
if yes,
i exchange values and keys first
sort by asc
i exchange keys and values again
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.
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 )