Page 1 of 1

Is this possible in one statement?

Posted: Thu Dec 01, 2005 2:23 pm
by InnerShadow
Hey,

I was wondering if there was a statement that would compare multiple values and return one of them based on coditions that can be specified?

Like if i told it to pick the highest number and then gave it 4, 7, 21, 35 and 2, it would return 35.

thanks

Posted: Thu Dec 01, 2005 2:28 pm
by Deemo
well there is always max() and min().

if you want it to have different conditions, like through a parameter, i am not sure it exists

what you could do is make your own function, and make it seem like strtotime() where one parameter is the way to get a value (for example "highest" or "lowest")

Posted: Thu Dec 01, 2005 2:29 pm
by pickle
This should do it:

Code: Select all

$myArray = array(1,4,35,21);
$highest_number = array_pop(sort($myArray));
[ Edit: Dang it! I searched for max() and min() and I couldn't find it. Chalk one up to incompetence :) ]

Posted: Thu Dec 01, 2005 3:38 pm
by protokol
pickle wrote:This should do it:

Code: Select all

$myArray = array(1,4,35,21);
$highest_number = array_pop(sort($myArray));
[ Edit: Dang it! I searched for max() and min() and I couldn't find it. Chalk one up to incompetence :) ]
Won't work anyway :D sort() returns a bool, not the sorted array.

Posted: Thu Dec 01, 2005 3:44 pm
by foobar
protokol wrote:
pickle wrote:This should do it:

Code: Select all

$myArray = array(1,4,35,21);
$highest_number = array_pop(sort($myArray));
[ Edit: Dang it! I searched for max() and min() and I couldn't find it. Chalk one up to incompetence :) ]
Won't work anyway :D sort() returns a bool, not the sorted array.
Then this'll work:

Code: Select all

$myArray = array(1,4,35,21);
sort($myArray);
$highest_number = array_pop($myArray);
... ya damn nitpicker! :wink:

Posted: Thu Dec 01, 2005 4:32 pm
by pickle
protokol wrote:Won't work anyway :D sort() returns a bool, not the sorted array.
Sonofa!!!! Should have caught up on that.

Posted: Thu Dec 01, 2005 5:01 pm
by Deemo
i still think max() is easier 8O

Posted: Thu Dec 01, 2005 5:05 pm
by pickle
Oh definitely - without question.

Posted: Thu Dec 01, 2005 5:07 pm
by foobar
Deemo wrote:i still think max() is easier 8O
No way!

Try this for simplicity:

Code: Select all

$array = array(4, 7, 21, 35, 2);

function funky_fresh ($array, $end = -1) {

  $out = $array;
  
  $done = true;
  $lastpair = 0;

  if ($end == -1) {
    $end = count($out);
  }
  
  for ($i = 0; $i < $end; $i++) {

    if ($out[$i] > $out[$i+1] && isset($out[$i+1])) {

      $one = $out[$i];
      $two = $out[$i+1];

      $out[$i] = $two;
      $out[$i+1] = $one;
      
      $done = false;
      $lastpair = $i;
    }
  }
  
  
  if ($done) return $out;
  
  return funky_fresh($out, $lastpair);
}


$new_array = array_values(funky_fresh($array));
$largest_index = sizeof($new_array)-1;

echo $new_array[$largest_index];
:lol:

I changed the function's name to make it more confusing. :wink:

Posted: Thu Dec 01, 2005 5:09 pm
by InnerShadow
lol....i'm thinking max() is the way to go