例如,如果我有:确定变量具有最高值和最低值
$person1 = "10";
$person2 = "-";
$person3 = "5";
我需要确定的最高数量的人,并用“W”前面加上他们的字符串,并确定与人最低的(数字)数量,并在前面加上他们的字符串以“L”
我试图输出:
$person1 = "W10";
$person2 = "-";
$person3 = "L5";
例如,如果我有:确定变量具有最高值和最低值
$person1 = "10";
$person2 = "-";
$person3 = "5";
我需要确定的最高数量的人,并用“W”前面加上他们的字符串,并确定与人最低的(数字)数量,并在前面加上他们的字符串以“L”
我试图输出:
$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);
希望有所帮助。它应该给你提示使用哪些函数。和平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);
工作很愉快!非常感谢!!!!!!!! – supercoolville
嘿,你可以追加它,但看到我的其他答案,以更好地实现你想要的回应。 –
我会把每一个变量到一个数组,然后使用数组等等rt功能。
$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];
人1的比分然后可以通过使用$people_sorted['person1']
我累了,但得到了一个错误“警告:不能使用标量值作为数组” – supercoolville
查看原始版本 – supercoolville
这是错误的。 arsort返回一个布尔值,所以它的返回值不能用于索引一个数组。更不用说'$ people'数组是联想的而且没有数字索引,因此你不能做'[0]'或'[2]'。 – nickb
下面是引用是一个可行的解决方案,将与任何工作人组合:
$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);
输出:
array(3) {
["person2"]=>
string(3) "W10"
["person1"]=>
int(4)
["person3"]=>
string(2) "L0"
}
这也适用!谢谢!! – supercoolville
'$ PERSON1 = 'W'。 $ PERSON1; $ person3 ='L'。 $ person3'? –
^如果$ person3的人数超过$ person1,那么这种方法无效。我需要*用PHP确定具有最高/最低编号的人。 – supercoolville
你可以将数据格式化为数组吗?那么它会超级简单 – 2012-06-05 03:09:36