0

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);
Kalreg
  • 931
  • 3
  • 12
  • 31

2 Answers2

1

You could just encase the code into a function:

function swap(&$arr){       //NOTE SIGN & meaning passing by reference.
  if ($arr['tree_height'] > $arr['tree_age']){
    $cache_age = $arr['tree_age'] ;

    $arr['tree_age'] = $arr['tree_height'] ;
    $arr['tree_height'] = $cache_age ;
  }
}


$r = array("tree_type" => 1, "tree_height" => 5, "tree_age" => 2);
swap($r);

var_dump($r);

Another elegant solution: (based on Is there a PHP function for swapping the values of two variables?)

function swap(&$arr){       //NOTE SIGN & meaning passing by reference.
  if ($arr['tree_height'] > $arr['tree_age']){
    list($arr['tree_height'], $arr['tree_age']) = array($arr['tree_age'], $arr['tree_height']);
  }
}
Community
  • 1
  • 1
sybear
  • 7,837
  • 1
  • 22
  • 38
  • ur answet also uses "cache_age" and works similiar as mine above so it is unfortunatelly not a solution of my problem – Kalreg Dec 15 '13 at 22:45
  • Just tell me, what's wrong in caching data? It is a good practice when you swap values of several variables. – sybear Dec 15 '13 at 22:47
  • And yes, it works in pretty much the same way as your solution, I just encased it into a function and passed array by reference, making it usable. – sybear Dec 15 '13 at 22:48
  • 1
    Added another solution. Check the answer. – sybear Dec 15 '13 at 22:51
  • Second solution is better although i read somewhere that using list is not a good solution because of speed of it but it is better. Tell me if i use reference then setting values to array that is referenced wouldnt change original array ? – Kalreg Dec 15 '13 at 22:53
  • 1
    That is the point: you desired a solution like `fname($r)`, which requires the use of passing by reference. Thus: passing by reference **will affect** the original array, otherwise you have to use `return` statement in your function. – sybear Dec 15 '13 at 22:55
0

This might work (not tested):

function fname(&$a, &$b) {

    if ($a <= $b)
        return;

    $t = $a;
    $a = $b;
    $b = $t;
}

fname($r['tree_height'], $r['tree_age']);

EDIT: Maybe You wanted something like this?

function fname(&$array, $name1, $name2) {

    if ($array[$name1] <= $array[$name2])
        return;

    $t = $array[$name1];
    $array[$name1] = $array[$name2];
    $array[$name2] = $t;
}

fname($r, 'tree_height', 'tree_age');
Roman Hocke
  • 4,137
  • 1
  • 20
  • 34