Array returned by SoapClient

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cluster28
Forum Newbie
Posts: 10
Joined: Mon May 24, 2010 5:29 am

Array returned by SoapClient

Post by cluster28 »

Hi every body, i´m new in the Forum and i´m from Spain.

I have a problem with a array returned by a WebService throw SoapClient. It returns an array that one of the keys is an string value "1". So PHP can not access to the following array inside.

For instance:

I receive this result doing

Code: Select all


$soap = new soapclient('wsdl',array('location'=>'https://www.domain.com/servlet/APIv3','trace'=>true,'exceptions'=>false));
$array = $soap->__soapCall('method', $data);
["recsonpage"]=> string(1) "1"
["recsindb"]=> string(1) "1"
["1"]=> array(18) {
["thawtecertstat"]=> string(2) "NA"
["websiteid"]=> string(8) "16494076"
["domainstat"]=> string(6) "Active"

If i would to access to "domainstat" i will write

Code: Select all

$array["1"]["domainstat"];
But key ["1"] it arrives in string type and PHP con not read numerical keys like string. Itself modify the string to int if we declare the array manually. And search for int key, like indexed array, but in this case is empty.

That´s only happen wirh arrays returned by SOAP.

Thank you.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Array returned by SoapClient

Post by Christopher »

Maybe try:

Code: Select all

$array[$array["recsonpage"]]["domainstat"];
(#10850)
cluster28
Forum Newbie
Posts: 10
Joined: Mon May 24, 2010 5:29 am

Re: Array returned by SoapClient

Post by cluster28 »

It should be

Code: Select all

$array[$array["1"]]["domainstat"];
but it doesn´t works.

Thak you anyway.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Array returned by SoapClient

Post by Christopher »

cluster28 wrote:It should be

Code: Select all

$array[$array["1"]]["domainstat"];
but it doesn´t works.

Thak you anyway.
No ... it shouldn't be that. In you example $array["recsonpage"] is a string with value "1". I was suggesting using that.
(#10850)
cluster28
Forum Newbie
Posts: 10
Joined: Mon May 24, 2010 5:29 am

Re: Array returned by SoapClient

Post by cluster28 »

I think i explained wrong. The array i had is (there are more fields but it´s reduced):

Code: Select all

array(3)
{
["recsonpage"]=> "1",
["recsindb"]=> "1",
["1"]=> array(3) {["thawtecertstat"]=> string(2) "NA", ["websiteid"]=> string(8) "16494076", ["domainstat"]=> string(6) "Active" }
}
If i would access to domainstat i would do that: $array["1"]["domainstat"] Correct?

Bur ["1"] is not a indexed key. Is a string type key (because Soap returns that format), and PHP can´t read it like a string. PHP think that ["1"] is index 1 and in $array is NULL.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Array returned by SoapClient

Post by AbraCadaver »

I can't reproduce this, but it seems to be a bug in several libraries in different versions of PHP, although I didn't find one mentioned for soapclient. You might try upgrading PHP. If that doesn't help or you can't upgrade, you may do something like this (not tested as I can't define a numeric string key):

Code: Select all

foreach($array as $k => $v) {
   if(is_numeric($k)) {
      $result[(int)$k] = $v;
   } else {
      $result[$k] = $v;
   }
}
Actually, PHP shouldn't let you build an array with a numeric string key, so this may work:

Code: Select all

foreach($array as $k => $v) {
      $result[$k] = $v;
}
You might also try this to see if it fixes it:

Code: Select all

ksort($array);
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
cluster28
Forum Newbie
Posts: 10
Joined: Mon May 24, 2010 5:29 am

Re: Array returned by SoapClient

Post by cluster28 »

I tried with ksort and neither works.

foreach method works perfectly bur i thought there would be better solution.

I will look about upgrade.

Thank you.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Array returned by SoapClient

Post by Weirdan »

AbraCadaver wrote: (not tested as I can't define a numeric string key)
I was able to build such array using json_decode and array cast:

Code: Select all

<?php
    $a = '{"1":"asd"}';
    var_dump(phpversion(), (array)json_decode($a));

Code: Select all

weirdan@virtual-debian: ~/tests$ php q.php
string(8) "5.2.12-2"
array(1) {
  ["1"]=>
  string(3) "asd"
}
weirdan@virtual-debian: ~/tests$ sudo update-alternatives --set php /usr/local/php/bin/php
update-alternatives: using /usr/local/php/bin/php to provide /usr/bin/php (php) in manual mode.
weirdan@virtual-debian: ~/tests$ php q.php
string(5) "5.3.1"
array(1) {
  ["1"]=>
  string(3) "asd"
}
weirdan@virtual-debian: ~/tests$
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Array returned by SoapClient

Post by AbraCadaver »

So these are hacks and maybe there is a better way to return your soap response, but here are several ways:

Code: Select all

$array = array_combine(array_keys($array), array_values($array));
echo $array[1];
Or this (but then the keys are renumbered starting at 0):

Code: Select all

shuffle($array);
echo $array[0];
Or use an object

Code: Select all

$object = (object) $soap->__soapCall('method', $data);
echo $object->{"1"};
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
cluster28
Forum Newbie
Posts: 10
Joined: Mon May 24, 2010 5:29 am

Re: Array returned by SoapClient

Post by cluster28 »

AbraCadaver wrote:So these are hacks and maybe there is a better way to return your soap response, but here are several ways:

Code: Select all

$array = array_combine(array_keys($array), array_values($array));
echo $array[1];
This one is very simply and works fine.

I upgraded soapclient and the problems persists.
Post Reply