0

这可能很简单,但我无能为力。

我正在使用 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

4 回答 4

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

}
于 2013-09-23T21:31:16.123 回答
0

您将需要使用 aforeach来循环查找正确的值

$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;
    }
}
于 2013-09-23T16:39:55.677 回答
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 );

让我知道这是否对您有帮助。

于 2013-09-24T03:10:57.500 回答
-1

对 Array 的简单转换将使 stdClassObject 立即可访问:

$myArray = (array)$myUnusableObject;

echo 'What have we here? '.var_export($myArray, true);
于 2013-09-20T20:29:50.023 回答