我的数组有问题。不知怎的,它不知道我的数组在哪里。我尝试使用以下代码:
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$pers_inventar = array();
$pers_id = $row['pers_id'];
$pers_person = array('birthday' => $row['pers_birthday'],'lastname' => $row['pers_lastname'],'firstname' => $row['pers_firstname'],'job' => $row['pers_job']);
$pers_inventar[$pers_id][] = $pers_person;
}
也许我的功能错了。谢谢你的帮助
答案 0 :(得分:2)
问题是你每次迭代都要清除你的数组。将数组声明放在循环之前。
$pers_inventar = array();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$pers_id = $row['pers_id'];
$pers_person = array('birthday' => $row['pers_birthday'],'lastname' => $row['pers_lastname'],'firstname' => $row['pers_firstname'],'job' => $row['pers_job']);
$pers_inventar[$pers_id][] = $pers_person;
}
答案 1 :(得分:0)
你可以这样试试:
$pers_inventar = array();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$pers_id = $row['pers_id'];
$pers_person = array('birthday' => $row['pers_birthday'],'lastname' => $row['pers_lastname'],'firstname' => $row['pers_firstname'],'job' => $row['pers_job']);
$pers_inventar[$pers_id][] = $pers_person;
}
最好的是,当你先添加一个空数组然后进入你的while循环时。
另一个建议:
为什么要创建一个数组来放入" pers_id"阵列? 为什么不创建你的" pers_id"数组并在那里添加信息。