Php and WS - array with one row

For example I have WebService function which returns:

A. array with more than 1 row:

$ws->item[0]->id = 99;
$ws->item[0]->name = 'test';
$ws->item[1]->id = 99;
$ws->item[1]->name = 'test';
return $ws;

PHP converts it to:

stdClass Object ( [item] => Array ( [0] => stdClass Object ( [id] => 99 [name] => 'test' ) [1] => stdClass Object ( [0] => stdClass Object ( [id] => 99 [name] => 'test' ) ) ) 

So it is array - correct - I can use foreach to iterate throw elements.

B. array with 1 row:

$ws->item[0]->id = 99;
$ws->item[0]->name = 'test';
return $ws;

PHP converts it to:

stdClass Object ( [item] => stdClass Object ( [id] => 99 [name] => 'test' ) ) 

So it isn’t ARRAY - I can’t using in that case foreach to iterate throw elements.

So I must always write duplicate code (to get elements) for array with 1 row and more than 1 row ??

Any better if you change

<xsd:element name="id" type="xsd:string" maxOccurs="unbounded"/>

to xsd:array ??

This is part of my file wsdl:


<xsd:element name="items" type="itemsType" />
	      
	        <xsd:complexType name="itemsType">
			  <xsd:complexContent>
			    <xsd:restriction base="SOAP-ENC:Array">
			      <xsd:sequence>
			        <xsd:element name="id" type="xsd:string" maxOccurs="unbounded"/>
			      </xsd:sequence>
			    </xsd:restriction>
			  </xsd:complexContent>
		    </xsd:complexType>
		    
		    
		    <xsd:element name="result" type="xsd:string" />

Server:


function search($tab)
	{
		return print_r($tab, true); 
	}

	$server = new SoapServer('http://localhost:82/soap_test/wsdl/wsdl.php'); 
	$server->addFunction("search"); 
	$server->handle(); 

client:


$tab_ws->id[0] = 'a235g';
		//$tab_ws->id[1] = 'b68fg';
		 				
		$client = new SoapClient('http://localhost:82/soap_test/wsdl/wsdl.php');
		$tab = $client->search($tab_ws);	
		print_r($tab);

Client show me result:

stdClass Object ( [id] => a235g ) 

But I need result:

stdClass Object ( [id] => Array ( [0] => a235g ) ) 

What am I doing wrong ?


<xsd:element name="id" type="xsd:array" maxOccurs="unbounded"/>

?? Still the same problem :frowning: