Jump to content


Photo

Extract Key Value in Multi Dimensional Array


  • Please log in to reply
7 replies to this topic

#1 willpower

willpower
  • Members
  • PipPipPip
  • Advanced Member
  • 296 posts
  • LocationWarrington, Cheshire, UK

Posted 02 March 2006 - 06:58 PM

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
Wait...I've got it...Bollocks, it's gone again!

If at first you don't succeed....stop using ASP and get onto PHP

#2 willpower

willpower
  • Members
  • PipPipPip
  • Advanced Member
  • 296 posts
  • LocationWarrington, Cheshire, UK

Posted 02 March 2006 - 08:45 PM

come on kenrbnsn ...i know that you can do this...lol

Will
Wait...I've got it...Bollocks, it's gone again!

If at first you don't succeed....stop using ASP and get onto PHP

#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 02 March 2006 - 09:29 PM

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

Try this
<?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'];
?>

Ken

#4 willpower

willpower
  • Members
  • PipPipPip
  • Advanced Member
  • 296 posts
  • LocationWarrington, Cheshire, UK

Posted 02 March 2006 - 10:32 PM

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
Wait...I've got it...Bollocks, it's gone again!

If at first you don't succeed....stop using ASP and get onto PHP

#5 willpower

willpower
  • Members
  • PipPipPip
  • Advanced Member
  • 296 posts
  • LocationWarrington, Cheshire, UK

Posted 03 March 2006 - 01:25 AM

bump
Wait...I've got it...Bollocks, it's gone again!

If at first you don't succeed....stop using ASP and get onto PHP

#6 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 03 March 2006 - 01:55 AM

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

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

#7 willpower

willpower
  • Members
  • PipPipPip
  • Advanced Member
  • 296 posts
  • LocationWarrington, Cheshire, UK

Posted 03 March 2006 - 02:18 AM

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

Wait...I've got it...Bollocks, it's gone again!

If at first you don't succeed....stop using ASP and get onto PHP

#8 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 03 March 2006 - 03:29 AM

Since the value you need is in the second level array, you need to do:
<?php
$member_no = $result[ 'handleSimpleUserUploadResult'][' 'MembershipNumber'];
?>

Ken




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users