【问题标题】:How do I retrieve the value of php stdClass Object如何检索 php stdClass 对象的值
【发布时间】:2013-09-20 20:24:28
【问题描述】:

这可能很简单,但我束手无策。

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

【问题讨论】:

  • 对于what is the value of LastName where the FirstName is equal to "JJ",您需要提供另一个示例,说明何时找到了多条记录。看起来在此搜索中您只得到一个结果。如果找到多个结果,则必须使用 foreach 才能找到正确的结果数组。
  • 好的@Pitchinnate,我有一个多结果返回的例子。我已将其添加到 OP。在此先感谢您的帮助。我应该在星期一之前解决这个问题。
  • @Pitchinnate,关于您的上述回复。我想知道如何从这些场景中的任何一个中检索值。请帮我。我在这个网站和其他网站上找到的解决方案似乎不起作用。

标签: php object stdclass


【解决方案1】:

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

【讨论】:

  • @Pithcinnate 感谢您的回复。我将该代码放在我的文件中,当我测试它时,它给了我这个错误 5x 警告:在第 28 行为 foreach() 提供的参数无效 第 28 行是每个的内部。其他建议给了我这个错误。为什么 PHP 不喜欢这种方式呢?
【解决方案2】:

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

}

【讨论】:

    【解决方案3】:

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

    $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 ); 来尝试返回多个结果

    如果这对你有帮助,请告诉我。

    【讨论】:

    • 顺便说一下,我只是从@Pitchinnate提供的示例扩展而来
    【解决方案4】:

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

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

    【讨论】:

    • 为什么数组比对象更容易访问?
    • 我猜我抽签太快了。我在使用 stdClassObjects 时遇到了问题,这些问题通过强制转换为 Array 被消除了,我认为这就是 OP 所拥有的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-17
    • 2011-08-31
    • 1970-01-01
    相关资源
    最近更新 更多