我正在使用此代码将数据库数据转换为json,但我在此行中获得了未定义的索引错误$ total [$ name] + = $ score。 提前谢谢。
<?php
require_once('conn.php');
$sql = "select name,score from highscores ";
$res = mysqli_query($con,$sql);
if (mysqli_num_rows($res) > 0) {
$result = array();
while($item = mysqli_fetch_assoc($res)){
$result[]=$item;
}
$total=array();
foreach ($result as $row) {
$name = $row['name'];
$score = $row['score'];
$total[$name] += $score;
$newArray = array();
foreach ($total as $name => $score) {
array_push($newArray,array('name' => $name, 'score' =>$score));
}
}
echo json_encode(array("result"=>$newArray));
}