Jump to content

Extract Key Value in Multi Dimensional Array


willpower

Recommended Posts

The response I receive from a web service app is in the form of an array. This is set as $result.

Heres the first part of code

$item=each($result);
print_r($item);

and this returns the following (sensitive data replaced

Array ( [1] => Array ( [Username] => aaaaa [Password] => bbbbb [Forename] => Alex [Surname] => ccccc [Email] => will@xxxx.com [Phone] => 01925xxxxxx[Mobile] => 0780 xxxxxxx [Address1] => 49 xxxxxxx[Address2] => Bxxxxxx [Address3] => Warrington [Address4] => Cheshire [Address5] => xxxxxx [MembershipNumber] => 000056 [PayPointId] => 00000146 ) [value] => Array ( [Username] => aaaaa[Password] => bbbbb [Forename] => Alex [Surname] => xxxxx[Email] => will@xxxxx.com [Phone] => 01925 xxxxx[Mobile] => 0780 xxxxxx[Address1] => 49 xxxxxx[Address2] => xxxxxxxx [Address3] => Warrington [Address4] => Cheshire [Address5] => xxxxxx[MembershipNumber] => 000056 [PayPointId] => 00000146 ) [0] => handleSimpleUserUploadResult [key] => handleSimpleUserUploadResult )

Now you will note that this is a multi dimensional array. For some reason the data seems to appear twice...but Im not that bothered.

I simple need to set $membership_no as the [MembershipNumber] value

I have tried but in vain....any help much appreciated.

Will
Link to comment
Share on other sites

How did you know I was going to answer this one??? :-)

Try this
[code]<?php
$test = Array ( '1' => Array('Username' => 'aaaaa',
                                      'Password' => 'bbbbb',
                                      'Forename' => 'Alex',
                                      'Surname' => 'ccccc',
                                      'Email' => 'will@xxxx.com',
                                      'Phone' => '01925xxxxxx',
                                      'Mobile' => '0780 xxxxxxx',
                                      'Address1' => '49 xxxxxxx',
                                      'Address2' => 'Bxxxxxx',
                                      'Address3' => 'Warrington',
                                      'Address4' => 'Cheshire',
                                      'Address5' => 'xxxxxx',
                                      'MembershipNumber' => '000056',
                                      'PayPointId' => '00000146'),
                    'value' => Array ('Username' => 'aaaaa',
                                      'Password' => 'bbbbb',
                                      'Forename' => 'Alex',
                                      'Surname' => 'xxxxx',
                                      'Email' => 'will@xxxxx.com',
                                      'Phone' => '01925 xxxxx',
                                      'Mobile' => '0780 xxxxxx',
                                      'Address1' => '49 xxxxxx',
                                      'Address2' => 'xxxxxxxx',
                                      'Address3' => 'Warrington',
                                      'Address4' => 'Cheshire',
                                      'Address5' => 'xxxxxx',
                                      'MembershipNumber' => '000056',
                                      'PayPointId' => '00000146'),
                '0' => 'handleSimpleUserUploadResult', 'key' => 'handleSimpleUserUploadResult' );
$membership_no = $test[1]['MembershipNumber'];
?>[/code]

Ken
Link to comment
Share on other sites

You would expect that to work...but for a reason beyond my comprhesion it doesn't.

The $result is populated by this piece of code

$result = $soap_proxy->handleSimpleUserUpload($param);

This is how I get the:

Array ( [1] => Array ( [Username] => aaaaa [Password] => bbbbb [Forename] => Alex [Surname] => ccccc [Email] => will@xxxx.com [Phone] => 01925xxxxxx[Mobile] => 0780 xxxxxxx [Address1] => 49 xxxxxxx[Address2] => Bxxxxxx [Address3] => Warrington [Address4] => Cheshire [Address5] => xxxxxx [MembershipNumber] => 000056 [PayPointId] => 00000146 ) [value] => Array ( [Username] => aaaaa[Password] => bbbbb [Forename] => Alex [Surname] => xxxxx[Email] => will@xxxxx.com [Phone] => 01925 xxxxx[Mobile] => 0780 xxxxxx[Address1] => 49 xxxxxx[Address2] => xxxxxxxx [Address3] => Warrington [Address4] => Cheshire [Address5] => xxxxxx[MembershipNumber] => 000056 [PayPointId] => 00000146 ) [0] => handleSimpleUserUploadResult [key] => handleSimpleUserUploadResult )

Now it is worth mentioning here that this is all driven by a SOAP Envelope Client (NuSOAP.php) request to a WS server. Although I can't see why that would make a difference. The $param is obviously populated and in turn populating the $result....but I can't extract ANY data from it....just keep getting blank returns

Will
Link to comment
Share on other sites

After you get the "$result", I assume you're doing a print_r($result). In order to format the output better, do a
[code]<?php echo '<pre>' . var_export($result,true) . '</pre>'; ?>[/code]

The output will also be valid PHP, so it can be cut and pasted into another program for debugging.

I've never played with NuSoap, so I can't help you there.

Can you post some of the code?

Ken
Link to comment
Share on other sites

Wow...the array returned looks normal now.

array (
'handleSimpleUserUploadResult' =>
array (
'Username' => 'Banthien.hEynL',
'Password' => 'dfrtMG',
'Forename' => 'Alex',
'Surname' => 'Banthien',
'Email' => 'will@e-rizon.com',
'Phone' => '01925 222 005',
'Mobile' => '0780 392 7771',
'Address1' => '49 Rushton Close',
'Address2' => 'Burtonwood',
'Address3' => 'Warrington',
'Address4' => 'Cheshire',
'Address5' => 'WA5 4HB',
'MembershipNumber' => '000094',
'PayPointId' => '00000184',
),
)

How do I now extract the membership Number to $membership_no

I tried

$member_no = $result['MembershipNumber']; //no result

and

$new_result= var_export($result,true);
$member_no = $new_result['MembershipNumber'];// returned a value of "a"?????

Any ideas?

Thanks so far


Will
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.