Reputation: 644
How to make a multidimensional array from mysql database Like
I have three table
Table1 Table3
| ID | GPN | PKGID | | GPN | Amt |
| 1 | A | PKG01 | | A | 10 |
| 2 | B | PKG02 | | A | 15 |
| 3 | C | PKG03 | | A | 20 |
| 4 | D | PKG04 | | A | 25 |
| A | 30 |
| B | 17 |
----------------------------- | D | 90 |
| B | 20 |
Table2 | B | 40 |
| GPN | Date | | D | 60 |
| A | 2016-09-10 | | B | 80 |
| A | 2016-09-18 | | B | 100 |
| B | 2016-09-10 | | C | 3 |
| B | 2016-09-11 | | C | 6 |
| B | 2016-09-12 | | C | 9 |
| C | 2016-10-12 | | C | 12 |
| C | 2016-10-13 | | C | 15 |
| C | 2016-10-14 | | D | 7 |
| D | 2016-09-10 | | D | 10 |
| D | 2016-10-13 | | D | 13 |
How i have to make a multidimensional array
For Example
Date = 2016-09-10
I have to get all the from above three table behalf of given date.
so the result will be this
Array
(
[0] => Array
(
[PKGID] => PKG01
[GPN] => A
[AMT] => Array
(
[0] => 10
[1] => 15
[2] => 20
[3] => 25
[4] => 30
)
)
[1] => Array
(
[PKGID] => PKG02
[GPN] => B
[AMT] => Array
(
[0] => 17
[1] => 20
[2] => 40
[3] => 60
[4] => 80
[5] => 100
)
)
[2] => Array
(
[PKGID] => PKG04
[GPN] => D
[AMT] => Array
(
[0] => 90
[1] => 60
[2] => 7
[2] => 10
[2] => 13
)
)
)
I am using php and mysql.
Is there any easiest mysql query to do that witch i have to execute one time because right now i am executing mysql query multiple time in loop to making that array
Upvotes: 1
Views: 1721
Reputation: 360842
Simplest way is
$arr = array();
while($row = fetch()) {
$arr[$row['key1']][$row['key2']][etc...][] = $row;
}
Just use whatever fields you want to key your array off AS keys while building the array.
Upvotes: 1