Solved

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

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

803 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