[英]How do I retrieve the value of php stdClass Object
這可能很簡單,但是我不知所措。
我正在使用API。 我聲明$ checkLead並獲得一個stdClassObject,它將在下面顯示。 我正在嘗試檢索數組的值。 在這種情況下,有一個記錄,但是將來可能會包含更多。
這是我print_r時打印的內容。
stdClass Object (
[result] => stdClass Object (
[count] => 1
[leadRecordList] => stdClass Object (
[leadRecord] => stdClass Object (
[Id] => 26
[Email] => test3@test.com
[ForeignSysPersonId] =>
[ForeignSysType] =>
[leadAttributeList] => stdClass Object (
[attribute] => Array (
[0] => stdClass Object (
[attrName] => FirstName
[attrType] => string
[attrValue] => JJ
)
[1] => stdClass Object (
[attrName] => LastName
[attrType] => string
[attrValue] => JJ
)
[2] => stdClass Object (
[attrName] => Website
[attrType] => url
[attrValue] => test.com
)
)
)
)
)
)
)
這是一個多結果返回的示例。
stdClass Object (
[result] => stdClass Object (
[count] => 2
[leadRecordList] => stdClass Object (
[leadRecord] => Array (
[0] => stdClass Object (
[Id] => 33
[Email] => test3@test.com
[ForeignSysPersonId] =>
[ForeignSysType] =>
[leadAttributeList] => stdClass Object (
[attribute] => Array (
[0] => stdClass Object (
[attrName] => FirstName
[attrType] => string
[attrValue] => jj )
[1] => stdClass Object (
[attrName] => LastName
[attrType] => string
[attrValue] => amonit )
[2] => stdClass Object (
[attrName] => Website
[attrType] => url
[attrValue] => test.com )
)
)
)
[1] => stdClass Object (
[Id] => 26
[Email] => test3@test.com
[ForeignSysPersonId] =>
[ForeignSysType] =>
[leadAttributeList] => stdClass Object (
[attribute] => Array (
[0] => stdClass Object (
[attrName] => FirstName
[attrType] => string
[attrValue] => bob )
[1] => stdClass Object (
[attrName] => LastName
[attrType] => string
[attrValue] => smith )
[2] => stdClass Object (
[attrName] => Phone
[attrType] => phone
[attrValue] => 123-123-1234 )
[3] => stdClass Object (
[attrName] => Website
[attrType] => url
[attrValue] => test.com )
)
)
)
)
)
)
)
因此,我想知道是否有人可以幫助我檢索FirstName
的值。 我還希望能夠通過說出“ FirstName
等於"JJ"
的LastName
的值是什么”來檢索該值。
您將需要使用foreach
來循環查找正確的值
$lastname = '';
foreach($checkLead->result->leadRecordList->leadRecord as $record) {
$attributes = $record->leadAttributeList->attribute;
$found = false;
$temp_lastname = '';
foreach($attributes as $attr) {
if($attr->attrName == 'LastName') {
$temp_lastname = $attr->attrValue;
}
if($attr->attrName == 'FirstName' && $attr->attrValue == 'JJ') {
$found = true;
}
}
if($found) {
$lastname = $temp_lastname;
break;
}
}
為了獲得名聲,您有機會從Marketo獲得多條線索,或者最好是一條線索。 這是獲取該信息的代碼。 希望這可以幫助。
if (is_object($checkLead)) {
$leadRecord = $checkLead->result->leadRecordList->leadRecord;
if (is_array($leadRecord)) {
//multiple leads returned
//run another foreach within a forloop to get the same values
for($x=0; $x<sizeof($leadRecord); $x++) {
$LeadAttribute = $leadRecord[$x]->leadAttributeList->attribute;
foreach($LeadAttribute as $attribute) {
if($attribute->attrName == 'FirstName') {
echo "\nFirst Name: ".$attribute->attrValue;
}
}
}
} else {
//single lead returned
$LeadAttribute = $leadRecord->leadAttributeList->attribute;
foreach($LeadAttribute as $attribute) {
if($attribute->attrName == 'FirstName') {
echo "\nFirst Name: ".$attribute->attrValue;
}
}
}
}
我相信您可以使用下面的代碼,它將按您期望的方式工作:
$lastname = '';
$leadRecord = $checkLead->result->leadRecordList->leadRecord;
if ( $checkLead->result->count > 1 ) {
foreach ( $leadRecord as $leadRecordItem ) {
foreach ( $leadRecordItem->leadAttributeList as $attributes ) {
$found = false;
$temp_lastname = '';
foreach( $attributes as $attr ) {
if($attr->attrName == 'LastName') {
$temp_lastname = $attr->attrValue;
}
if($attr->attrName == 'FirstName' && $attr->attrValue == 'JJ') {
$found = true;
}
}
if($found) {
$lastname = $temp_lastname;
break;
}
}
}
}
else {
foreach ( $leadRecord->leadAttributeList as $attributes ) {
$found = false;
$temp_lastname = '';
foreach( $attributes as $attr ) {
if($attr->attrName == 'LastName') {
$temp_lastname = $attr->attrValue;
}
if($attr->attrName == 'FirstName' && $attr->attrValue == 'JJ') {
$found = true;
}
}
if($found) {
$lastname = $temp_lastname;
break;
}
}
}
echo $lastname;
我從頭開始構建了兩個對象,並嘗試了此代碼,最后,它回顯了“ JJ”-我想這就是您想要的,至少是要緊抓它並修改您的特定需求。
萬一您想按我的方式嘗試,以下是構建單次和多次結果返回的方法:
function to_object( $arr ) {
return is_array( $arr ) ? (object) array_map(__FUNCTION__, $arr) : $arr;
}
$checkLeadArr2 = array(
'result' => array(
'count' => 2,
'leadRecordList' => array(
'leadRecord' => array(
array(
'Id' => 33,
'Email' => 'test3@test.com',
'ForeignSysPersonId' => null,
'ForeignSysType' => null,
'leadAttributeList' => array(
'attribute' => array(
array(
'attrName' => 'FirstName',
'attrType' => 'string',
'attrValue' => 'JJ',
),
array(
'attrName' => 'LastName',
'attrType' => 'string',
'attrValue' => 'amonit',
),
array(
'attrName' => 'Website',
'attrType' => 'url',
'attrValue' => 'test.com',
),
)
)
),
array(
'Id' => 26,
'Email' => 'test3@test.com',
'ForeignSysPersonId' => null,
'ForeignSysType' => null,
'leadAttributeList' => array(
'attribute' => array(
array(
'attrName' => 'FirstName',
'attrType' => 'string',
'attrValue' => 'JJ',
),
array(
'attrName' => 'LastName',
'attrType' => 'string',
'attrValue' => 'JJ',
),
array(
'attrName' => 'Website',
'attrType' => 'url',
'attrValue' => 'test.com',
),
)
)
),
)
)
)
);
$checkLeadArr1 = array(
'result' => array(
'count' => 1,
'leadRecordList' => array(
'leadRecord' => array(
'Id' => 26,
'Email' => 'test3@test.com',
'ForeignSysPersonId' => null,
'ForeignSysType' => null,
'leadAttributeList' => array(
'attribute' => array(
array(
'attrName' => 'FirstName',
'attrType' => 'string',
'attrValue' => 'JJ',
),
array(
'attrName' => 'LastName',
'attrType' => 'string',
'attrValue' => 'JJ',
),
array(
'attrName' => 'Website',
'attrType' => 'url',
'attrValue' => 'test.com',
),
)
)
)
)
)
);
$checkLead = to_object( $checkLeadArr1 );
您只需要將最后一個代碼放在我之前給您的foreach循環之上,就可以嘗試通過從$checkLead = to_object( $checkLeadArr1 );
切換來返回多結果$checkLead = to_object( $checkLeadArr1 );
和$checkLead = to_object( $checkLeadArr2 );
讓我知道這是否對您有幫助。
簡單地轉換為Array應該可以立即訪問stdClassObject:
$myArray = (array)$myUnusableObject;
echo 'What have we here? '.var_export($myArray, true);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.