Jump to content

Archived

This topic is now archived and is closed to further replies.

willpower

Extract Key Value in Multi Dimensional Array

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

Share this post


Link to post
Share on other sites
come on kenrbnsn ...i know that you can do this...lol

Will

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites
Since the value you need is in the second level array, you need to do:
[code]<?php
$member_no = $result[ 'handleSimpleUserUploadResult'][' 'MembershipNumber'];
?>[/code]

Ken

Share this post


Link to post
Share on other sites

×

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.