I have a database with a number of entries with data like so:
id number data
1 1 'tyfvib'
2 1 'fgdhjjd'
3 1 'gdgdhdj'
4 2 'dgfhfh'
5 2 'fghdhd'
So I have a unique ID and then a column with numbers and then a column with different strings.
I then have a query like this:
$sql = "SELECT * FROM table";
$stmt = DB::run($sql);
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
$number= $row['number'];
$data = $row['data'];
}
I want to make a multidimensional associative array which would have first of all an array of the unique numbers and these would contain an array of the data associated with them.
I had tried putting $array[$number] = $data
inside the while loop but this obviously will just make an array where the only data associated with a number is the last one in the loop.
I had tried putting $array[$number] = $data inside the while loop but this obviously will just make an array where the only data associated with a number is the last one in the loop.
Then add an additional set of []:
$array[$number][] = $data;
will create a new entry in the array $array[$number]
each time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With