我在PHP中显示它们时遇到一些问题。我的代码:
$query = ****;
$row = mysqli_num_rows($query);
if($row > 0) {
while ($result = mysqli_fetch_assoc($query)){
$awardname = trim($result['awardname']);
$name = trim($result['preferred_name']).' '.trim($result['surname']);
$arr = array();
$array[$awardname] = $name;
}
foreach($array as $awardname => $names ){
echo $awardname. ' given to: <br />';
foreach($names as $name){
echo $name;
}
}
}
我要做的就是显示奖项和他们下面的收件人的名字(一个名字在另一个旁边或下面) 我收到的错误是:
第32行:foreach($names as $name){
你们有人可以提出不正确的建议吗?
答案 0 :(得分:1)
您只需要在数组声明中添加[]
即可。以下是一些改进:
while ($result = mysqli_fetch_assoc($query)){
$array[trim($result['awardname'])][]=trim($result['preferred_name']).' '.trim($result['surname']);
}
foreach($array as $awardname=>$names){
echo $awardname,' given to: <br />',implode(', ',$names),'<br />';
}