Is this possible in one statement?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
InnerShadow
Forum Commoner
Posts: 37
Joined: Thu Nov 10, 2005 10:44 pm
Location: US

Is this possible in one statement?

Post 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
Deemo
Forum Contributor
Posts: 418
Joined: Sun Jan 18, 2004 11:48 am
Location: Washington DC

Post 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")
Last edited by Deemo on Thu Dec 01, 2005 2:29 pm, edited 1 time in total.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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 :) ]
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post 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.
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Post 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:
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

protokol wrote:Won't work anyway :D sort() returns a bool, not the sorted array.
Sonofa!!!! Should have caught up on that.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Deemo
Forum Contributor
Posts: 418
Joined: Sun Jan 18, 2004 11:48 am
Location: Washington DC

Post by Deemo »

i still think max() is easier 8O
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Oh definitely - without question.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Post 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:
InnerShadow
Forum Commoner
Posts: 37
Joined: Thu Nov 10, 2005 10:44 pm
Location: US

Post by InnerShadow »

lol....i'm thinking max() is the way to go
Post Reply