Page 1 of 1

merging to the nTH dimension of a multi-dimentional array

Posted: Sat Sep 17, 2011 2:54 pm
by wafflemann
This may be an odd topic but no where on the internet and in the looking through the php manual can I think of a way of doing this.. (maybe I'm just being stupid and missing the obvious but I thought I'd come to the nice people of this forum and ask.

Basically I have a multi-dimensional array that I don't know how many dimensions it will grow to. I have an idea but writing repeating while loops to cover this is just stupid so I was wondering if there was a functional way of doing this.

heres the print_r or the array-->

Code: Select all

Array
(
    [0] => Array
        (
            [table_name] => cust
            [0] => id
            [1] => firstname
            [2] => lastname
            [3] => soc
            [4] => address
            [5] => Array
                (
                    [field_name] => _r_zipcode_id
                    [related_table_name] => zipcode
                    [0] => id
                    [1] => zipcode
                    [2] => city
                    [3] => _r_state_id
                )

            [6] => phonenum
            [7] => extention
            [8] => employer
            [9] => employerphone
            [10] => Array
                (
                    [field_name] => _r_banks_id
                    [related_table_name] => banks
                    [0] => id
                    [1] => bankname
                )

            [11] => bankaccountnum
            [12] => driverlic
            [13] => Array
                (
                    [field_name] => driverlic_r_state_id
                    [related_table_name] => state
                    [0] => id
                    [1] => state_name
                    [2] => state_abr
                )

            [14] => supervisor
            [15] => directdeposit
            [16] => payperiod
            [17] => lastpayday
            [18] => monthlyincome
            [19] => coapplicname
            [20] => coapplicsoc
            [21] => coapplicphone
            [22] => coapplicrelation
            [23] => applicationdate
        )

    [1] => Array
        (
            [table_name] => zipcode
            [0] => id
            [1] => zipcode
            [2] => city
            [3] => Array
                (
                    [field_name] => _r_state_id
                    [related_table_name] => state
                    [0] => id
                    [1] => state_name
                    [2] => state_abr
                )

        )

)

What this is is a simple database pulled out from mysql. It's put in array to show the relationships and the related tables.
On the second dimension at number five(I think that how you reference it.. I'm basically self taught here so scold me if I'm wrong) you can see under the [related_table name] "zipcode" _r_state_id is a reference to another stable called state. However it's not expanded like the rest.

I did expand it down at the bottom of the array so you can see it. But basically I want to expand it further up so everything "flows" together in away.
Now I could write another nested while loop that would easily do the trick however not knowing how many related tables will be under another once I get this database further down the line.. I don't wanna keep modifying the dbms.

So as I said I was looking to do write a function that would just handle this for me (duh) however I can't think of a way to just append onto an array. Yes there array_merge and such functions but that doesn't help in this scenario what so ever. For instead of just merging array I want to create an entirely new dimension. Is there any way to go about this in php? Writing a function that would dynamically add dimensions to an already existing array without me manually doing it? After looking around I'm almost afraid to say that php is too simple of a language to do so as compared to c or java.

Just thought I'd stop in here and ask. Any help is much appreciated!!