这段代码出了什么问题,需要提问谢谢。 我使用了另一种对我来说很好的方法,但是我的团队负责人说这不好,因为我们需要一个匹配的用户输入结果在提交后回显。所以我想出了类似的东西。但它仍然无法正常工作。
<?php
//One of this should be my Results after input submited.
$type = array(
0 => array(
"val" => 0,
"title" => "Du bist Weltenbummler"
),
1 => array(
"val" => 0,
"title" => "Luxusliebhaber"
),
2 => array(
"val" => 0,
"title" => "Naturbursche/-madle"
),
3 => array(
"val" => 0,
"title" => "Sunnyboy/-girl"
),
4 => array(
"val" => 0,
"title" => "Wasserratte"
),
5 => array(
"val" => 0,
"title" => "Wellnessliebhaber"
),
6 => array(
"val" => 0,
"title" => "Adrenalinjunkie"
),
7 => array(
"val" => 0,
"title" => "Städtebummler"
)
);
//this are my post data which is being looped to store the user input value
$type = "";
$current = 0;
$exp = explode(',', $_POST['expenditure']);
foreach($exp as $key => $arrE){
$arrEx = $arrE;
if($arrE < 300) {
$type[0]["val"] += 0;
$type[1]["val"] += 0.1;
$type[2]["val"] += 0.4;
$type[3]["val"] += 0.3;
$type[4]["val"] += 0.7;
$type[5]["val"] += 1;
$type[6]["val"] += 0.7;
$type[7]["val"] += 1;
}
if($arrEx >= 300 && $arrEx <= 500) {
$type[0]["val"] += 0;
$type[1]["val"] += 1;
$type[2]["val"] += 0.4;
$type[3]["val"] += 1;
$type[4]["val"] += 0.7;
$type[5]["val"] += 1;
$type[6]["val"] += 0.7;
$type[7]["val"] += 1;
}
if($arrEx >= 600 && $arrEx <= 800) {
$type[0]["val"] += 1;
$type[1]["val"] += 1;
$type[2]["val"] += 0.4;
$type[3]["val"] += 1;
$type[4]["val"] += 0.7;
$type[5]["val"] += 1;
$type[6]["val"] += 0.7;
$type[7]["val"] += 1;
}
if($arrEx >= 900 && $arrEx <= 1000) {
$type[0]["val"] += 2;
$type[1]["val"] += 1.4;
$type[2]["val"] += 0.4;
$type[3]["val"] += 1;
$type[4]["val"] += 0.7;
$type[5]["val"] += 1.3;
$type[6]["val"] += 1.4;
$type[7]["val"] += 1.5;
}
if($arrEx >= 1500 && $arrEx <= 3000) {
$type[0]["val"] += 2;
$type[1]["val"] += 1.4;
$type[2]["val"] += 0.4;
$type[3]["val"] += 1;
$type[4]["val"] += 0.7;
$type[5]["val"] += 1.3;
$type[6]["val"] += 2.2;
$type[7]["val"] += 2.1;
}
}
$Season = explode(',', $_POST['seasonString']);
foreach($Season as $key => $arrS){
$arrSe = $arrS;
if( $arrSe == "1"){
$type[0]["val"] += 1;
$type[1]["val"] += 1.4;
$type[2]["val"] += 0.4;
$type[3]["val"] += 1;
$type[4]["val"] += 2.0;
$type[5]["val"] += 2.0;
$type[6]["val"] += 2.0;
$type[7]["val"] += 2.0;}
if( $arrSe == "2"){
$type[0]["val"] += 0.5;
$type[1]["val"] += 1.1;
$type[2]["val"] += 1.4;
$type[3]["val"] += 1.0;
$type[4]["val"] += 2.0;
$type[5]["val"] += 2.0;
$type[6]["val"] += 2.0;
$type[7]["val"] += 2.0;}
if( $arrSe == "3"){
$type[0]["val"] += 0.5;
$type[1]["val"] += 1.1;
$type[2]["val"] += 1.4;
$type[3]["val"] += 1.0;
$type[4]["val"] += 2.0;
$type[5]["val"] += 2.0;
$type[6]["val"] += 2.0;
$type[7]["val"] += 2.0;}
if( $arrSe == "4"){
$type[0]["val"] += 0.5;
$type[1]["val"] += 1.1;
$type[2]["val"] += 1.4;
$type[3]["val"] += 0.5;
$type[4]["val"] += 2.0;
$type[5]["val"] += 2.0;
$type[6]["val"] += 2.0;
$type[7]["val"] += 2.0;
}
}
for($i=0;$i<count($type);$i++){
//First loop
if($i == 0) {
$title = $type[0]["title"];
$current = $type[0]["val"];
}
if($type[$i]["val"] > $current) {
$title = $type[$i]["title"];
}
}
echo $type;
?>
答案 0 :(得分:0)
完全猜测......
$index = 0;
$max = $type[0]["val"];
for($i=1; $i<count($type); $i++) {
if($type[$i]["val"] > $max) {
$index = $i;
$max = $type[$i]["val"];
}
}
echo "Du bist " . $type[$index]["title"];
或者,甚至更简单:
$maxItem = $type[0];
for($i = 1; $i < count($type); $i++) {
if($type[$i]["val"] > $maxItem["val"]) {
$maxItem = $type[$i];
}
}
echo "Du bist " . $maxItem["title"];