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

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

mwheeler_fsdAsked:
Who is Participating?
 
Ray PaseurCommented:
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
 
mwheeler_fsdAuthor Commented:
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
 
Ray PaseurCommented:
Hey, Mike - thanks for catching that (good eye!) and thanks for using EE.  All the best, ~Ray
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.