Log in

View Full Version : Array Value



bluewalrus
09-30-2009, 04:22 AM
Using the php min array function is there a way to get the position that the min value came from? I was thinking of something with the count but can't seem to get it. For an example


$value[0]= 12.84930;
$value[1]=9989.12123;
$value[2]=.0023412093;
$value[3]=10.19;
$value[4]=2.56;

echo min($value); //But instead of .0023412093 it returns 2.

djr33
09-30-2009, 06:10 AM
You could do it in two steps:
Get that min value, then find the key from that resulting value.
http://www.php.net/manual/en/function.array-search.php

Directly, though, I'm not sure how that would work.

You could also use sort() or another similar function and then of course the key would be 0 for that lowest value.

JasonDFR
09-30-2009, 07:42 AM
$value = array();
$value[0]= 12.84930;
$value[1]=9989.12123;
$value[2]=.0023412093;
$value[3]=10.19;
$value[4]=2.56;

echo array_search(min($value), $value); // This will return the key, 2 in this case.

I don't think there is a one shot function. You'll have to combine at least two core php functions.

forum_amnesiac
09-30-2009, 08:00 AM
This follows on from what djr33 had to say


<?php
$value[0]= 12.84930;
$value[1]=9989.12123;
$value[2]=.0023412093;
$value[3]=10.19;
$value[4]=2.56;
$val=min($value);

$key=array_search($val, $value);
echo "position = ".$key;
?>

djr33
09-30-2009, 09:02 AM
And a correction: this will return the first key that matches the value, so if there are duplicates that might get confusing.

JasonDFR
09-30-2009, 09:21 AM
I really enjoy problems like this.


$value = array();
$value[0]= 12.84930;
$value[1]=9989.12123;
$value[2]=.0023412093;
$value[3]=10.19;
$value[4]=2.56;
$value[5]=.0023412093;
$value[6]=.0023412093;

function minValueKeys($array) {

$minValueKeys = array();
$minValue = min($array);
do {
$key = array_search($minValue, $array);
$minValueKeys[] = $key;
unset($array[$key]);
} while (in_array($minValue, $array));

return $minValueKeys;
}

var_dump(minValueKeys($value));

...and a perfect problem for the seldom used do ... while loop! I love it.

techietim
09-30-2009, 10:44 AM
Using the php min array function is there a way to get the position that the min value came from? I was thinking of something with the count but can't seem to get it. For an example


$value[0]= 12.84930;
$value[1]=9989.12123;
$value[2]=.0023412093;
$value[3]=10.19;
$value[4]=2.56;

echo min($value); //But instead of .0023412093 it returns 2.
EDIT:
Sorry. The question was badly phrased.

I just copied and pasted that exact code and ran it on my computer, and it's showing me 0.0023412093. Make sure you're running PHP 5 <=, as there could be a bug related to this in earlier versions.

JasonDFR
09-30-2009, 10:53 AM
I just copied and pasted that exact code and ran it on my computer, and it's showing me 0.0023412093. Make sure you're running PHP 5 <=, as there could be a bug related to this in earlier versions.

Bluewalrus' question is poorly worded. I came to the same conclusion you did Tim before realizing that what he probably wants is a way to get the key of the lowest value in the array, not the lowest value.