Solved

Loop PHP Multidimensional Array and insert into MySQL

Posted on 2014-03-04
6
4,270 Views
Last Modified: 2014-03-04
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):

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] => 
)

Open in new window


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!
0
Comment
Question by:Carlos Elgueta
  • 3
  • 3
6 Comments
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39904079
<?php // demo/temp_newcoit.php
error_reporting(E_ALL);


// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28380000.html


/**
 * For each returned record, I need to populate columns for accountName, createdDate, reason, type, room, checkInDate, checkOutDate, waitingListDate, and guestType.
 */

// TEST DATA FROM THE POST AT EE
$soap = 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' => NULL,
)
;

// DOES IT LOOK RIGHT?
echo '<pre>';
print_r($soap);

// POPULATE THE FIELDS
$data = $soap['data'][0];
$qry = new StdClass;

// accountName
$qry->accountName = $data['accountName'];

// createdDate
$qry->createdDate = $data['createdDate'];

// reason
foreach ($data['definedValues'] as $arr)
{
    if ($arr['fieldName'] != 'Reason') continue;
    $qry->reason = $arr['value'];
    break;
}

// type
foreach ($data['definedValues'] as $arr)
{
    if ($arr['fieldName'] != 'Type') continue;
    $qry->type = $arr['value'];
    break;
}

// room
foreach ($data['definedValues'] as $arr)
{
    if ($arr['fieldName'] != 'Room') continue;
    $qry->room = $arr['value'];
    break;
}

// checkInDate
foreach ($data['definedValues'] as $arr)
{
    if ($arr['fieldName'] != 'Check in Date') continue;
    $qry->checkInDate = $arr['value'];
    break;
}

// checkOutDate
foreach ($data['definedValues'] as $arr)
{
    if ($arr['fieldName'] != 'Check Out Date') continue;
    $qry->checkOutDate = $arr['value'];
    break;
}

// waitingListDate
foreach ($data['definedValues'] as $arr)
{
    if ($arr['fieldName'] != 'Date Placed on Waiting List') continue;
    $qry->waitingListDate = $arr['value'];
    break;
}

// guestType
foreach ($data['definedValues'] as $arr)
{
    if ($arr['fieldName'] != 'Guest Type') continue;
    $qry->guestType = $arr['value'];
    break;
}

// HAVE WE GOT THEM ALL?  YES.
print_r($qry);

Open in new window

0
 
LVL 1

Author Comment

by:Carlos Elgueta
ID: 39904187
That is fantastic!  And fast!

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!
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39904275
Yes, I think you're on firm ground there.  Something like foreach ($soap['data'] as $data) {
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:Carlos Elgueta
ID: 39904327
great - thank you.
0
 
LVL 1

Author Closing Comment

by:Carlos Elgueta
ID: 39904333
Perfect.
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39904339
Thanks for the points and thanks for using EE, ~Ray
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Ajax and PHP 4 47
How to extract database info from current month and year 7 34
MySQL Persistent Connections 10 35
What is define("__APPROOT__", __DIR__); 6 32
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question