PHP SOAP stdclass对象数组

PHP SOAP stdclass对象数组

问题描述:

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>
";
    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]?

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);
}

I used the information above and came up with the following, tested and working.

echo " <br> Available Groups <table> <tr><th>Group ID</th><th>First Name</th><th>Middle Name</th><th>Last Name</th><th>Role</th></tr>";
    foreach($xml->Staff as $staff )
    {
        $staffId = $staff->StaffID;
        $lastName = $staff->Last;
        $firstName = $staff->First;
        $middleName = $staff->Middle;
        $role = $staff->Role;

        $html = "<tr>";
        $html .= "<td>".$staffId."</td>";
        $html .= "<td>".$firstName."</td>";
        $html .= "<td>".$middleName."</td>";
        $html .= "<td>".$lastName."</td>";
        $html .= "<td>".$role."</td>";
        $html .= "</tr>";
        echo $html;
    }
    echo "</table>";

I also used information from the following post. iterating through a stdClass object in PHP