I am looking for elegant way to sort two of three values stored in one array. The third one can be ignored but i dont want to loose it (so unseting is not an option).
Imagine such array:
$r = Array("tree_type" => 1, "tree_height" = 5, "tree_age" = 2);
If tree_height is bigger then it's age i want to swap tree height with tree_age, so tree height is always smaller number then age.
Now I am sorting it like this:
if ( $r['tree_height'] > $r['tree_age'] ) {
$tmp = $r['tree_height'];
$r['tree_height'] = $r['tree_age'];
$r['tree_age'] = $tmp;
}
It works perfectly fine, but i am looking for more elegant way. The best solution would be sth like this:
fname($r, $r['tree_height'], $r['tree_age']);
fname would always swap second argument with third if it's bigger then third, otherwise would do nothing.
Any advice would be appreciated. Kalreg.
ANSWER:
The shortest answer, without condition is:
$tmp = Array($r['tree_height'], $r['tree_age'])
sort($tmp);