Solved

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

Posted on 2012-04-05
3
246 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 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
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
Comment Utility
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 108

Expert Comment

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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now