如何检索php stdClass对象的值

时间:2013-09-20 20:24:29

标签: php object stdclass

这可能很简单,但我最终会陷入困境。

我正在使用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的值。我还希望能够通过说出LastName等于FirstName等于"JJ"的价值来检索价值。

4 个答案:

答案 0 :(得分:0)

您需要使用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;
    }
}

答案 1 :(得分:0)

要获得名字,您有可能从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;
            }
        }
    }

}

答案 2 :(得分:0)

我相信你可以使用下面的代码,它会按你期望的方式工作:

$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 );

来尝试返回多个结果

如果有帮助,请告诉我。

答案 3 :(得分:-1)

对Array的简单强制转换应该可以立即访问stdClassObject:

$myArray = (array)$myUnusableObject;

echo 'What have we here? '.var_export($myArray, true);