php - Determining the variables with the highest and lowest values -
for example if have:
$person1 = "10"; $person2 = "-"; $person3 = "5"; i need determine person highest number , prepend string "w" , determine person lowest (numeric) number , prepend string "l"
i trying output:
$person1 = "w10"; $person2 = "-"; $person3 = "l5";
$persons = array(10, '-', '12', 34 ) ; //array of persons, define $max_index = array_search($max = max($persons), $persons); $min_index = array_search($min = min($persons), $persons); $persons[$max_index] = 'w' . $persons[$max_index]; $persons[$min_index] = 'l' . $persons[$min_index]; print_r($persons); hope helps. should give hints on functions use. peace danuel
solution 2
foreach((array)$persons $index=>$value){ if(!is_numeric($value))continue; if(!isset($max_value)){ $max_value = $value; $max_index = $index; } if(!isset($min_value)){ $min_value = $value; $min_index = $index; } if( $max_value < $value ){ $max_value = $value; $max_index = $index; } if( $min_value > $value ){ $min_value = $value; $min_index = $index; } } @$persons[$max_index] = 'w'.$persons[$max_index];//@suppress errors in case @$persons[$min_index] = 'l'.$persons[$min_index]; print_r($persons);
Comments
Post a Comment