April 16, 2012

Multidimensional array with many sub arrays and mysql – how to deal with it?

Question by Delicja

I have very long multidimensional array that have many sub arrays. I would like to insert some value from them into mysql. I would be gratefull for some tip how to deal with it. I would like to use a loop that put only some value to one or different table in database. How I can get value from for example [Things][Thing][k][value]? Thanks for any advice.

Array
(
[Data] => Array
    (
        [A] => Array
            (
                [B] => Array
                    (
                        [0] => Array
                            (
                                [C] => Array
                                    (
                                        [value] => some value1
                                    )
                                [D] => Array
                                    (
                                        [value] => some value2
                                    )
                                [E] => some value3
                            )
                        [1] => Array
                            (
                                [C] => Array
                                    (
                                        [value] => some value4
                                    )
                                [D] => Array
                                    (
                                        [value] => some value5
                                    )
                                [E] => 5
                            )
                    )
                [value] => 
            )
[Things] => Array
            (
                [Thing] => Array
                    (
                        [F] => Array
                            (
                                [value] => some value6
                            )
                        [G] => Array
                            (
                                [H] => Array
                                    (
                                        [0] => Array
                                            (
                                                [i] => Array
                                                    (
                                                        [value] => some value7
                                                    )
                                                [j] => Array
                                                    (
                                                        [value] => some value8
                                                        [value] => some value8
                                                    )
                                                [k] => Array
                                                    (
                                                        [value] => some value9
                                                    )
                                                [l] => Array
                                                    (
                                                        [value] => some value10
                                                    )
                                                [m] => some value11
                                                [n] => 
                                            )
                                        [1] => Array
                                            (
                                                [o] => Array
                                                    (
                                                        [value] => some value12
                                                    )
                                                [p] => Array
                                                    (
                                                        [value] => some value13
                                                    )
                                                [r] => Array
                                                    (
                                                        [value] => some value14
                                                    )
                                                [d] => Array
                                                    (
                                                        [value] => some value15
                                                    )
                                                [t] => some value16
                                                [u] => 
                                            )
                                    )
                                [value] => 
                            )
                            )
                            )           
                    [Thing2] => Array       
                    (

                            (...)

                            and so on...

In my database I have table for these things from array above. There is id in it of course and some other fields that are connected with other tables.
As you can see below I would like to get some values from array, insert it into Table Thing, and some of them put into
Table for field 4 and Table for field 5 and get id of them and put it into Table Thing. I’ll use stored procedure.
I would like to call it from php (I hope that this conception is good?). I have problem to get values that I want to get from this array so David Chan I’m looking for help with array and loop. I tried as Starx has written but I got nothing (white screen). I’m still learning so I know that I have to do something wrong.

............          .................       ..................
Table Thing           Table for field 4       Table for field 5

id int                id int                  id int
field 1 varchar       value varchar           value varchar
field 2 varchar
field 3 varchar       ................        ................
field 4 int
field 5 int
(...)
...........

Answer by Starx

Whenever you are trying to access multidimensional array, you have to use indices to point a particular field.

For example:

$data['A']['B'][0]['C']['VALUE'] would give you the value some value1

Use a similar way to get the values and insert into the database.

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!