I\'m trying to get hold of the largest value in an array, while still preserving the item labels. I know I can do this by running sort(), but if I do so I simply lose the la
<?php
$array = array("a"=>1,"b"=>2,"c"=>4,"d"=>5);
foreach ($array as $key => $value) {
if ($value >= $max)
$max = $key;
}
echo " The array in largest number :".$max."<br/>";
?>
$ee = array('a' => 50, 'b' => 25, 'c' => 5, 'd' => 80, 'e' => 40, 'f' => 152, 'g' => 45, 'h' => 28);
$Acurr = '';
$Amax = 0;
foreach($ee as $key => $value) {
$Acurr = $value;
if($Acurr >= $Amax) {
$Amax = $Acurr;
}
}
echo "greatest number is $Amax";
If you just want the largest value in the array use the max function. This will return the largest value, although not the corresponding key. It does not change the original array.
If you care about the the key you could then do
$key = array_search(max($array), $array)
(Edited to include @binaryLV's suggestion)
greatestValue=> try this its very easy
$a=array(10,20,52,105,56,89,96);
$c=0;
foreach($a as $b)
{
if($b>$c)
$c=$b;
}
echo $c;