作为费用数据分析的一部分,我试图找出每个月的前3个费用类别。现在我有3个月的数据:数组public class Client {
public Client() {
}
public void clientConnect() throws UnknownHostException, IOException {
System.out.println("enter your username");
Scanner scanner = new Scanner(System.in);
String msg = scanner.nextLine();
Socket socket = new Socket("localhost", 9090);
PrintWriter writer = new PrintWriter(socket.getOutputStream(), true);
writer.println(msg);
}
}
。
现在我正在使用下面的代码及其正常工作
$month0,$month1,$month3
我试图在此处使用$month0 = array("Bank"=>$Bank[0], "CreditCard"=>$CreditCard[0], "Loan"=>$Loan[0],"Household"=>$Household[0],"Utilities"=>$Utilities[0]);
$month1 = array("Bank"=>$Bank[1], "CreditCard"=>$CreditCard[1], "Loan"=>$Loan[1],"Household"=>$Household[1],"Utilities"=>$Utilities[1]);
$month2 = array("Bank"=>$Bank[2], "CreditCard"=>$CreditCard[2], "Loan"=>$Loan[2],"Household"=>$Household[2],"Utilities"=>$Utilities[2]);
arsort($month0 );
arsort($month1 );
arsort($month2 );
$top3month0 = array_slice($month0, 0, 3);
$top3month1 = array_slice($month1, 0, 3);
$top3month2 = array_slice($month2, 0, 3);
foreach ($top3month0 as $category => $amount){
$cat_top3month0[] = $category;
$amt_top3month0[] = $amount;
}
foreach ($top3month1 as $category => $amount){
$cat_top3month1[] = $category;
$amt_top3month1[] = $amount;
}
foreach ($top3month2 as $category => $amount){
$cat_top3month2[] = $category;
$amt_top3month2[] = $amount;
}
//For Month0
echo "Top 1 category is ".$cat_top3month0[0]." and amount: ".$amt_top3month0[0];
echo "Top 2 category is ".$cat_top3month0[1]." and amount: ".$amt_top3month0[1];
echo "Top 3 category is ".$cat_top3month0[2]." and amount: ".$amt_top3month0[2];
//For Month1
echo "Top 1 category is ".$cat_top3month1[0]." and amount: ".$amt_top3month1[0];
echo "Top 2 category is ".$cat_top3month1[1]." and amount: ".$amt_top3month1[1];
echo "Top 3 category is ".$cat_top3month1[2]." and amount: ".$amt_top3month1[2];
//For Month3
echo "Top 1 category is ".$cat_top3month2[0]." and amount: ".$amt_top3month2[0];
echo "Top 2 category is ".$cat_top3month2[1]." and amount: ".$amt_top3month2[1];
echo "Top 3 category is ".$cat_top3month2[2]." and amount: ".$amt_top3month2[2];
来减少代码的长度。我更改了上面的代码,如下所示,使用for循环
for loop
但是上面的代码不起作用。查看调试日志时,我看到以下错误
for ($i = 0; $i<3; $i++){
$month[i] = array("Bank"=>$Bank[i], "CreditCard"=>$CreditCard[i], "Loan"=>$Loan[i],"Household"=>$Household[i],"Utilities"=>$Utilities[i]);
arsort($month[i] );
$top3month[i] = array_slice($month[i], 0, 3);
foreach ($top3month[i] as $category => $amount){
$cat_top3month[i][] = $category;
$amt_top3month[i][] = $amount;
}
}
//For Month0
/echo "Top 1 category is ".$cat_top3month0[0]." and amount: ".$amt_top3month0[0];
/echo "Top 2 category is ".$cat_top3month0[1]." and amount: ".$amt_top3month0[1];
/echo "Top 3 category is ".$cat_top3month0[2]." and amount: ".$amt_top3month0[2];
//For Month1
echo "Top 1 category is ".$cat_top3month1[0]." and amount: ".$amt_top3month1[0];
echo "Top 2 category is ".$cat_top3month1[1]." and amount: ".$amt_top3month1[1];
echo "Top 3 category is ".$cat_top3month1[2]." and amount: ".$amt_top3month1[2];
//For Month3
echo "Top 1 category is ".$cat_top3month2[0]." and amount: ".$amt_top3month2[0];
echo "Top 2 category is ".$cat_top3month2[1]." and amount: ".$amt_top3month2[1];
echo "Top 3 category is ".$cat_top3month2[2]." and amount: ".$amt_top3month2[2];
针对行
PHP Warning: Use of undefined constant i - assumed 'i' (this will throw an Error in a future version of PHP)
我是PHP新手,仍然在学习。有人可以告诉我代码问题吗?或建议一种更好的方法?
更新:
更新了代码,将$month[i] = array("Bank"=>$Bank[i], "CreditCard"=>$CreditCard[i], "Loan"=>$Loan[i],"Household"=>$Household[i],"Utilities"=>$Utilities[i]);
纠正为i
$i
现在我有以下错误:
for ($i = 0; $i<3; $i++){
$month[$i] = array("Bank"=>$Bank[$i], "CreditCard"=>$CreditCard[$i], "Loan"=>$Loan[$i],"Household"=>$Household[$i],"Utilities"=>$Utilities[$i]);
arsort($month[$i] );
$top3month[$i] = array_slice($month[$i], 0, 3);
foreach ($top3month[$i] as $category => $amount){
$cat_top3month[$i][] = $category;
$amt_top3month[$i][] = $amount;
}
}
//For Month0
echo "Top 1 category is ".$cat_top3month0[0]." and amount: ".$amt_top3month0[0];
echo "Top 2 category is ".$cat_top3month0[1]." and amount: ".$amt_top3month0[1];
echo "Top 3 category is ".$cat_top3month0[2]." and amount: ".$amt_top3month0[2];
//For Month1
echo "Top 1 category is ".$cat_top3month1[0]." and amount: ".$amt_top3month1[0];
echo "Top 2 category is ".$cat_top3month1[1]." and amount: ".$amt_top3month1[1];
echo "Top 3 category is ".$cat_top3month1[2]." and amount: ".$amt_top3month1[2];
//For Month3
echo "Top 1 category is ".$cat_top3month2[0]." and amount: ".$amt_top3month2[0];
echo "Top 2 category is ".$cat_top3month2[1]." and amount: ".$amt_top3month2[1];
echo "Top 3 category is ".$cat_top3month2[2]." and amount: ".$amt_top3month2[2];
针对行:
PHP Notice: Array to string conversion in
和
$month[$i] = array("Bank"=>$Bank[$i], "CreditCard"=>$CreditCard[$i], "Loan"=>$Loan[$i],"Household"=>$Household[$i],"Utilities"=>$Utilities[$i]);
反对PHP Fatal error: Uncaught Error: Only variables can be passed by reference in