For example if I have:
$person1 = "10";
$person2 = "-";
$person3 = "5";
I need to determine the person with the highest number and prepend their string with a "W" and also determine the person with the lowest (numeric) number and prepend their string with a "L"
I am trying to output:
$person1 = "W10";
$person2 = "-";
$person3 = "L5";
$persons = array(10, '-', '12', 34 ) ; //array of persons, you define this
$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 that helps. It should give you hints on what functions to use. Peace Danuel
foreach((array)$persons as $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 some errors just in case
@$persons[$min_index] = 'L'.$persons[$min_index];
print_r($persons);
I would put each of the variables into an array and then use the array sort function.
$people = array (
'person1' => $person1,
'person2' => $person2,
'person3' => $person3
);
asort($people);
$f = key($people);
end($people);
$l = key($people);
$people[$f] = 'L' . $people[$f];
$people[$l] = 'W' . $people[$l];
Person 1's score could then be reference by using $people_sorted['person1']
Here is a working solution that will work with any combination of people:
$people = array (
'person1' => 4,
'person2' => 10,
'person3' => 0
);
arsort( $people); // Sort the array in reverse order
$first = key( $people); // Get the first key in the array
end( $people);
$last = key( $people); // Get the last key in the array
$people[ $first ] = 'W' . $people[ $first ];
$people[ $last ] = 'L' . $people[ $last ];
var_dump( $people);
Output:
array(3) {
["person2"]=>
string(3) "W10"
["person1"]=>
int(4)
["person3"]=>
string(2) "L0"
}