1

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

4
  • trivial solution would be using group_concat in the query and exploding later when fetching Commented Aug 3, 2016 at 18:39
  • group_concat how to do that sir Commented Aug 3, 2016 at 18:40
  • Table2 appears to be irrelevant to the output you're trying to get. Commented Aug 3, 2016 at 19:41
  • Table2 here i have to check how many GPN on that day Commented Aug 3, 2016 at 20:16

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Sir What will be mysql query
^ ... the plot thickens, as it were.
Yes your are right @MarcB but by posting this question i want to know easiest mysql query witch i have to execute one time because right now i am executing mysql query multiple time in loop to making that array. and i am still learning sir as @ bwoebi is suggested me about group_concat i am reading about that and other similar function. i am concentrating to not to post rubbish kind of question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.