Get array elements\foreach error

I am getting an array of records from an asp.net webservice and wanting to display them in a dynamically created html table in the php page. I am new to php and not sure how to get the values from the array. I get an “invalid argument error in for each” error message. If I run the program and inspect the $res, I can see all the records in the TMemberListRec. I tried to use TMemberListRec in the for each to access them, but still get an error. Below is my php code and below that is the xml from the webservice. Can someone help me out with the code? If I want to password protect the webservice how do I pass the userid and passord to the webservice?

TIA

{
$wsdl=“http://www.domain.com/WebService/WS.asmx?WSDL”;
$client = new nusoapclient($wsdl, true);
$res = $client->call(‘GetMemberList’, array());

foreach ($hits->GetMemberListResult as $hit) {
$Company = html_entities($hit->aCompany);
$CompanyID = html_entities($hit->aCompanyID);

print <<< HTML

$Company

ID: $CompanyID

Price: $hit->aFirstName

_HTML_; }

}

</s:element>
<s:element name=“GetMemberList”>
<s:complexType />
</s:element>
<s:element name=“GetMemberListResponse”>
<s:complexType>
<s:sequence>
<s:element minOccurs=“0” maxOccurs=“1” name=“GetMemberListResult” type=“tns:ArrayOfTMemberListRec” />
</s:sequence>
</s:complexType>
</s:element>
<s:complexType name=“ArrayOfTMemberListRec”>
<s:sequence>
<s:element minOccurs=“0” maxOccurs=“unbounded” name=“TMemberListRec” type=“tns:TMemberListRec” />
</s:sequence>
</s:complexType>
<s:complexType name=“TMemberListRec”>
<s:sequence>
<s:element minOccurs=“0” maxOccurs=“1” name=“aContactID” type=“s:string” />
<s:element minOccurs=“0” maxOccurs=“1” name=“aFirstName” type=“s:string” />
<s:element minOccurs=“0” maxOccurs=“1” name=“aLastName” type=“s:string” />
<s:element minOccurs=“0” maxOccurs=“1” name=“aCompany” type=“s:string” />
</s:sequence>
</s:complexType>
</s:schema>

Is $hits->GetMemberListResult an array? (I am thinking Not).

The format of the “Foreach” function is

[php]
//

foreach (array_expression as $value) {
//Statements of code (typically using $value)

}
//
[/php]

If you need to use the “Index” of the array as well, you can do the following

[php]
//

foreach (array_expression as $key => $value) {
//Statements of code (typically using $value)
//Now $key is also available

}
//
[/php]

peg110:

Based on the information I provided, what would be the correct wat to get the data from the array? I changed the foreach and the error messsage went away and it runs, but I don’ t get any values back.

foreach ($res as $value) {
$Company = $value->aCompany;
$this->ediResult2->Text = $Company;
}

TIA

[php]
$res = $client->call(‘GetMemberList’, array());

foreach ($res as $hit) {
$Company = html_entities($hit->aCompany); // Only possible if $res is an array of objects
$CompanyID = html_entities($hit->aCompanyID); // Only possible if $res is an array of objects

print <<< HTML

<b>$Company</b><br/>
ID: $CompanyID<br/>
Price: $hit->aFirstName<br/>
</div>

HTML;
}
[/php]

This would solve the foreach issue. Be careful when assuming that you get objects returned: if not, you cannot use the notation you’re using right now. If so, the class definition should be available (I think).

Sponsor our Newsletter | Privacy Policy | Terms of Service