如何检索php stdClass对象的值
这可能很简单,但我却束手无策 我正在使用一个API。我声明$checkLead并获得一个stdClassObject,它将显示在下面。我正在尝试检索数组的值。在本例中,有一条记录,但将来可能会包含更多记录 这是我打印时打印的内容如何检索php stdClass对象的值,php,object,stdclass,Php,Object,Stdclass,这可能很简单,但我却束手无策 我正在使用一个API。我声明$checkLead并获得一个stdClassObject,它将显示在下面。我正在尝试检索数组的值。在本例中,有一条记录,但将来可能会包含更多记录 这是我打印时打印的内容 stdClass Object ( [result] => stdClass Object ( [count] => 1 [leadRecordList] => stdClass Object (
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
的值。我还希望能够通过这样的语句来检索该值:“LastName
的值是多少,FirstName
等于“JJ”
”一个简单的数组转换将使stdClassObject立即可访问:
$myArray = (array)$myUnusableObject;
echo 'What have we here? '.var_export($myArray, true);
您需要使用
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获得多个lead,或者最好是一个lead。以下是获取该信息的代码。希望这对您有所帮助
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;
}
}
}
}
if(is_对象($checkLead)){
$leadRecord=$checkLead->result->leadRecordList->leadRecord;
if(is_数组($leadRecord)){
//返回多个线索
//在forloop中运行另一个foreach以获得相同的值
对于($x=0;$xleadAttributeList->attribute;
foreach($LeadAttribute作为$attribute){
如果($attribute->attrName=='FirstName'){
echo“\n名称:”.$attribute->attrValue;
}
}
}
}否则{
//单导线返回
$LeadAttribute=$leadRecord->leadAttributeList->attribute;
foreach($LeadAttribute作为$attribute){
如果($attribute->attrName=='FirstName'){
echo“\n名称:”.$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($checkLeadArr2);
如果这对您有帮助,请告诉我。对于
当FirstName等于“JJ”时,LastName的值是多少"
您需要提供另一个示例,说明何时找到了多条记录。在本次搜索中,您似乎只得到了一个结果。如果找到了多条记录,则将有一个结果数组,您必须使用foreach来查找正确的一条。好的@PitchInner,我有一个多条结果返回的示例。我有一个将其添加到OP。提前感谢您的帮助。我应该在周一之前对此有一个解决方案,关于您的上述响应。我想知道如何从这两种情况中检索值。请帮助我。我在该网站和其他网站上找到的解决方案似乎不起作用。为什么数组更有效比对象更容易访问?我想我画得太快了。我在使用stdClassObjects时遇到了一些问题,这些对象是通过对数组的强制转换消除的,我认为OP就是这样做的。@谢谢您的响应。我把代码放在了我的文件中,当我测试它时,它给了我这个错误5x警告:无效参数su第28行的applied for foreach()第28行是每个的内部。其他建议给了我这个错误。为什么PHP不喜欢这样做呢?顺便说一句,我只是从@PitchInner提供的示例进行了扩展