Carlos Elgueta
asked on
Loop PHP Multidimensional Array and insert into MySQL
Hi All,
Having a heck of a time looping through a multidimensional array so I can insert values into a MySQL database. I have read through foreach and it's just not sinking in (Coldfusion guy, sorry!)
I'm consuming a NuSOAP resource, and I'm getting a multidimensional array back. I need to be able to loop each returned "record" and populate a MySQL table with the values. This is the response I'm getting (print_r):
For each returned record, I need to populate columns for accountName, createdDate, reason, type, room, checkInDate, checkOutDate, waitingListDate, and guestType.
The subarrays might not always be in the same order, depending on what is filled out in the web application and what is not, so I have to do some sort of check --> if fieldName = 'whatever' set thisValue = value.
Any help would be greatly appreciated!
Having a heck of a time looping through a multidimensional array so I can insert values into a MySQL database. I have read through foreach and it's just not sinking in (Coldfusion guy, sorry!)
I'm consuming a NuSOAP resource, and I'm getting a multidimensional array back. I need to be able to loop each returned "record" and populate a MySQL table with the values. This is the response I'm getting (print_r):
Array
(
[count] => 1
[data] => Array
(
[0] => Array
(
[accountName] => Test User
[accountRef] => 24281.0.5672027
[attachments] =>
[createdDate] => 2014-02-26T21:43:32.796Z
[date] => 2014-02-26T05:00:00.000Z
[definedValues] => Array
(
[0] => Array
(
[dataType] => 0
[displayType] => 1
[fieldName] => Reason
[fieldRef] => 24281.0.5693551
[value] => General Use
[valueRef] => 24281.0.5709983
)
[1] => Array
(
[dataType] => 0
[displayType] => 1
[fieldName] => Type
[fieldRef] => 24281.0.5686363
[value] => Guest
[valueRef] => 24281.0.5686365
)
[2] => Array
(
[dataType] => 0
[displayType] => 1
[fieldName] => Room
[fieldRef] => 24281.0.5685410
[value] => Training Room
[valueRef] => 24281.0.5685412
)
[3] => Array
(
[dataType] => 1
[displayType] => 0
[fieldName] => Check in Date
[fieldRef] => 24281.0.5694549
[value] => 2/25/2014
[valueRef] => 24281.0.7318589
)
[4] => Array
(
[dataType] => 1
[displayType] => 0
[fieldName] => Check Out Date
[fieldRef] => 24281.0.5685368
[value] => 2/26/2014
[valueRef] => 24281.0.7318587
)
[5] => Array
(
[dataType] => 1
[displayType] => 0
[fieldName] => Date Placed on Waiting List
[fieldRef] => 24281.0.5685388
[value] => 2/14/2014
[valueRef] => 24281.0.7318588
)
[6] => Array
(
[dataType] => 0
[displayType] => 1
[fieldName] => Guest Type
[fieldRef] => 24281.0.5684375
[value] => Vendor
[valueRef] => 24281.0.5684377
)
)
[final] =>
[immutable] =>
[lastModifiedDate] => 2014-02-28T21:34:11.956Z
[note] => Guest
[ref] => 24281.0.7156819
[type] => 1
)
)
[pages] => 1
[start] => 0
[total] => 1
[usedCache] =>
)
For each returned record, I need to populate columns for accountName, createdDate, reason, type, room, checkInDate, checkOutDate, waitingListDate, and guestType.
The subarrays might not always be in the same order, depending on what is filled out in the web application and what is not, so I have to do some sort of check --> if fieldName = 'whatever' set thisValue = value.
Any help would be greatly appreciated!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Yes, I think you're on firm ground there. Something like foreach ($soap['data'] as $data) {
ASKER
great - thank you.
ASKER
Perfect.
Thanks for the points and thanks for using EE, ~Ray
ASKER
Dumb question though - I need to loop the top level also. What would that syntax look like? And where do I declare the index? (Meaning how do I change $data = $soap['data'][0] to be $data = $soap['data'][x] depending on the number of top level records returned?
Thank you so much for your help!