britchey britchey - 1 month ago 10
PHP Question

PHP SOAP stdclass obect array

I am using PHP SOAP script to retrieve data from a web service. My PHP code shown below results in a stdclass obect which includes an array called Staff.

$values = $client->GetAllStaff($search_query);

$xml = $values->SasReqRespGetAllStaff;

print "<pre>\n";
print_r($xml);
print "</pre>";
?>


This produces the following result on in my web browser.


"stdClass Object
(
[Source] => R5 Sim
[RespMsgTag] => 5
[RespAck] => OK
[RespNote] =>
[TotalItems] => 13
[Staff] => Array
(
[0] => stdClass Object
(
[StaffID] => 15
[Last] => LastName1
[First] => TESTstaffOut1
[Middle] => MName1
[Role] => Level 1
)
[1] => stdClass Object
(
[StaffID] => 16
[Last] => LastName2
[First] => TESTstaffOut2
[Middle] => MName2
[Role] => Level 2
)"



How can I write the values from [Staff] => Array into PHP variables that can be used later or for other PHP? How can I loop through Staff [0], [1]?

sg- sg-
Answer

They are already in PHP variables (contained within the stdClass object). However if you want to loop through them you can do so like this:

foreach($xml->Staff as $staff)
{
  // Eg: To get the staff id:
  $staffId = $staff->StaffID;
  var_dump($staffId);
}
Comments