0

I am using PHP SOAP script to retrieve data from a web service. My PHP code shown below results in a stdclass object 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, which can be used later and/or for other PHP? How can I loop through Staff [0], [1]?

MFerguson
  • 1,739
  • 9
  • 17
  • 30
britchey
  • 37
  • 3

2 Answers2

0

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);
}
sg-
  • 2,196
  • 1
  • 15
  • 16
  • Thanks, this is what I was looking for! Is this how I would put the Staff stdclass obect into a MySql database? – britchey Oct 30 '16 at 16:00
  • Basically, yes. This will allow you to query the data. The other way is that you could serialize it and store it in the db that way (not queryable) – sg- Oct 30 '16 at 21:33
0

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

Community
  • 1
  • 1
britchey
  • 37
  • 3