如何使用PHP处理此对象?

时间:2016-03-04 13:21:21

标签: php object

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

我是PHP的新手,我已经向某些服务发出了SOAP请求,我的结果如上所示。我的目标是最终将EmployeeIdFirstNameLastNameGroupId存储在MYSQL表中。

我试过这样的事情:

foreach ($response->{'Record'} as $obj){    
    //$line = $obj->{'ConsId'}."\t".$obj->{'ConsName'}->{'FirstName'}."\t".$obj->{'ConsName'}->{'LastName'};                
    //echo $line."<br>";   
}

1 个答案:

答案 0 :(得分:2)

在您给出的示例中,无需使用foreach。只需使用对象变量就可以了:

$EmployeeId = $response->Record->EmployeeId;
$FirstName = $response->Record->BioInfo->FirstName;
$LastName = $response->Record->BioInfo->LastName;
$GroupId = $response->Record->GroupId; //receives the array