Solved

Php replace key names in a multi-dimensional array from a reference array

Posted on 2012-04-05
3
261 Views
Last Modified: 2012-04-05
Dear Experts,

I have developed a script that consumes an external web service using Php with Nusoap. The returned payload from the web service ($soapResponseData) has rather non intuitive names for the data elements we are most interested in, specifically - E130015, E130016, etc. so I created a reference array ($elementBusinessNameReference) that has a more descriptive name for what the returned data elements represent.

What I am attempting to do is replace the cryptic element names (E130013, etc.) with a more descriptive business name (LastName, etc.) before I return the result to the calling system.

Of course I thought this would be simple and straight forward in the beginning, but the solution continues to elude me. With added time pressure to complete the script, I am to a point where I feel I am over complicating the situation and would greatly appreciate a fresh look, and your expertise, in solving the issue.

A couple of details:

1. There are several different function calls that can be made to the web service, so the return ($soapResponseData) could contain a variety of returned elements depending on the call.

2. Depending on the function call, I create the reference array ($elementBusinessNameReference) from a DB table that contains all of the possible return values. This array will contain any possible returned element name, and its corresponding business name, based on the function that was called.

Rather than cloud my question with my failed attempts, I am providing array examples to illustrate my desired result.

Thank you in advance,
Mike

print_r($soapResponseData);

Array
(
    [DataSvcVer] => 1.0
    [MsgUUID] => 12345678-1234-1234-1234-123456789999
    [Svc] => Array
        (
            [SvcParms] => Array
                (
                    [ApplID] => Not Available
                    [SvcID] => Not Available
                    [SvcVer] => Not Available
                )
 
            [MsgData] => Array
                (
                    [ResponseData] => Array
                        (
                            [E130013] => DOE             
                            [E130014] => JOHN 
                            [E130015] => 1234 ANYWHERE ST
                            [E130016] => NEW YORK   
                            [E130017] => NY
                            [E130018] => 10017
                        )
 
                )
 
            [ErrCde] => -1
            [ErrMsg] => 
        )
 
    [ErrCde] => 0
    [ErrMsg] => Success
)


print_r($elementBusinessNameReference);

Array
(
    [E130001] => AccountNumber
    [E130002] => AccountStatus
    [E130013] => LastName
    [E130014] => FirstName
    [E130015] => Address
    [E130016] => City
    [E130017] => State
    [E130018] => ZipCode
    [E130019] => HomePhone
    [E130020] => BusinessPhone
)


Desired Result

Array
(
    [DataSvcVer] => 1.0
    [MsgUUID] => 12345678-1234-1234-1234-123456789999
    [Svc] => Array
        (
            [SvcParms] => Array
                (
                    [ApplID] => Not Available
                    [SvcID] => Not Available
                    [SvcVer] => Not Available
                )
 
            [MsgData] => Array
                (
                    [ResponseData] => Array
                        (
                            [LastName] => DOE             
                            [FirstName] => JOHN 
                            [Address] => 1234 ANYWHERE ST
                            [City] => NEW YORK   
                            [State] => NY
                            [ZipCode] => 10017
                        )
 
                )
 
            [ErrCde] => -1
            [ErrMsg] => 
        )
 
    [ErrCde] => 0
    [ErrMsg] => Success
)

Open in new window

0
Comment
Question by:mwheeler_fsd
  • 2
3 Comments
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 37813811
Without your data I cannot test this, so please feel free to correct my typos.  HTH, ~Ray
// CONSTRUCT A NEW ARRAY      
$new = array();

// USE AN ITERATOR TO ACCESS EACH OF THE ORIGINAL KEY => VALUE PAIRS
foreach ($soapResponseData["Svc"]["MsgData"]["ResponseData"] as $key = $val)
{
    // IF THE ORIGINAL KEY IS IN THE NAME REFERENCE
    if (array_key_exists($key, $elementBusinessNameReference))
    {
        // USE THE MATCHING DATA VALUE FOR THE NEW KEY
        $new[$elementBusinessNameReference[$key]] = $val;
    }
    else
    {
        // STRAY KEYS STAY THE SAME
        $new[$key] = $val;
    }
}

// REPLACE THE INNER ARRAY
$soapResponseData["Svc"]["MsgData"]["ResponseData"] = $new;

Open in new window

0
 

Author Closing Comment

by:mwheeler_fsd
ID: 37814543
Dear Ray,

Elegant and perfect! Well done!!

Sorry for the delay in my response, the test region for the service I am testing against was unavailable for a period this afternoon.

One small note, for those of you wishing to use this solution (and in no way a criticism!), please note that the line:

foreach ($soapResponseData["Svc"]["MsgData"]["ResponseData"] as $key = $var)

has a small typo. $key = $var should be $key => $var - notice the ">" after the "=" for the assignment.

Many thanks and much gratitude! Full points awarded.

Best regards,
--Mike
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 37814589
Hey, Mike - thanks for catching that (good eye!) and thanks for using EE.  All the best, ~Ray
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying 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

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

829 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