Reputation:
I have a problem with my array. Somehow it doesn't understand where my array is. I tried with the following code:
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;
}
Maybe my function is wrong. Thanks for any help
Upvotes: 2
Views: 37
Reputation: 27295
The problem is you clear your array every iteration. Put your array declaration before the loop.
$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;
}
Upvotes: 2
Reputation: 341
You could try it like this:
$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;
}
The best is, when you add an empty array first and then go into your while loop.
Another suggestion:
Why do you create an array to put in your "pers_id" array? Why don't you just create your "pers_id" array and add the information right in there.
Upvotes: 0