Is this possible in one statement?
Moderator: General Moderators
-
InnerShadow
- Forum Commoner
- Posts: 37
- Joined: Thu Nov 10, 2005 10:44 pm
- Location: US
Is this possible in one statement?
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
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
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")
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.
This should do it:
[ Edit: Dang it! I searched for max() and min() and I couldn't find it. Chalk one up to incompetence
]
Code: Select all
$myArray = array(1,4,35,21);
$highest_number = array_pop(sort($myArray));Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
- protokol
- Forum Contributor
- Posts: 353
- Joined: Fri Jun 21, 2002 7:00 pm
- Location: Cleveland, OH
- Contact:
Won't work anywaypickle wrote:This should do it:[ Edit: Dang it! I searched for max() and min() and I couldn't find it. Chalk one up to incompetenceCode: Select all
$myArray = array(1,4,35,21); $highest_number = array_pop(sort($myArray));]
Then this'll work:protokol wrote:Won't work anywaypickle wrote:This should do it:[ Edit: Dang it! I searched for max() and min() and I couldn't find it. Chalk one up to incompetenceCode: Select all
$myArray = array(1,4,35,21); $highest_number = array_pop(sort($myArray));]
sort() returns a bool, not the sorted array.
Code: Select all
$myArray = array(1,4,35,21);
sort($myArray);
$highest_number = array_pop($myArray);No way!Deemo wrote:i still think max() is easier
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];I changed the function's name to make it more confusing.
-
InnerShadow
- Forum Commoner
- Posts: 37
- Joined: Thu Nov 10, 2005 10:44 pm
- Location: US