-1
object(stdClass)#5 (1) { 
    ["Record"]=> object(stdClass)#6 (3) { 
        ["EmployeeId"]=> int(2637944) 
        ["BioInfo"]=> object(stdClass)#7 (2) { 
            ["FirstName"]=> string(6) "Aprony" 
            ["LastName"]=> string(1) "!" 
        } 
        ["GroupId"]=> array(38) { 
            [0]=> int(2601) 
            [1]=> int(2661) 
            [2]=> int(2663) 
            [3]=> int(3481) 
            [4]=> int(3602)
        } 
    } 
} 

I'm new to PHP and I have made a SOAP request to some service and my result are see above. My goal is to eventually store EmployeeId, FirstName, LastName and the GroupIds in a MYSQL table.

I have tried to so something like:

foreach ($response->{'Record'} as $obj){    
    //$line = $obj->{'ConsId'}."\t".$obj->{'ConsName'}->{'FirstName'}."\t".$obj->{'ConsName'}->{'LastName'};                
    //echo $line."<br>";   
}
morcen
  • 368
  • 2
  • 14
bingwa
  • 99
  • 1
  • 1
  • 9
  • You can use [get_object_vars()](http://php.net/manual/en/function.get-object-vars.php) if you don't know what the properties are going to be, but for something like a SOAP response you should already know the structure. – Dezza Mar 04 '16 at 13:27
  • You already correctly got to `$response->{'Record'}`. That again is an `object(stdClass)`. Do the same thing again you did for the first step... – deceze Mar 04 '16 at 13:31
  • You can convert it to array so that you can process it easily. Please refer given link:http://stackoverflow.com/questions/2476876/how-do-i-convert-an-object-to-an-array – Indrajit Mar 04 '16 at 13:36

1 Answers1

2

In your given example, there's no need to use foreach. Simply using the object variables should work:

$EmployeeId = $response->Record->EmployeeId;
$FirstName = $response->Record->BioInfo->FirstName;
$LastName = $response->Record->BioInfo->LastName;
$GroupId = $response->Record->GroupId; //receives the array
morcen
  • 368
  • 2
  • 14
  • This seems to work but if I'm processing a handful of objects I sure need to have some form of a loop...thanks – bingwa Mar 04 '16 at 13:57
  • That is correct, but I can only base my answer on your given scenario. Should you need help with something closer to your actual scenario, update your question and I will update my answer. – morcen Mar 04 '16 at 14:03