【问题标题】:Read var_dump data with PHP使用 PHP 读取 var_dump 数据
【发布时间】:2012-03-02 11:20:14
【问题描述】:

我可以知道如何在下面的 PHP 示例中检索每个数组中的 givenNmae 和 middlename 值吗?谢谢你。

object(stdClass)#13 (1) 
{ ["contact"]=> array(157) { 
    [0]=> object(stdClass)#14 (6) { 
        ["created"]=> string(20) "2007-10-27T04:54:45Z" 
        ["updated"]=> string(20) "2007-10-27T04:54:45Z" 
        ["uri"]=> string(73) "http://social.yahooapis.com/v1/user/SAFWRTHXMZPTISKTIEJPYKLW4I/contact/557" 
        ["isConnection"]=> string(5) "false" 
        ["id"]=> string(2) "57" 
        ["fields"]=> array(2) { 
            [0]=> object(stdClass)#15 (8) { 
                ["created"]=> string(20) "2007-10-27T04:54:45Z" 
                ["updated"]=> string(20) "2007-10-27T04:54:45Z" 
                ["uri"]=> string(85) "http://social.yahooapis.com/v1/user/SAFWRTHXMZPTISKTIEJPYKLW4I/contact/57/otherid/1351" 
                ["id"]=> string(3) "131" 
                ["type"]=> string(7) "otherid" 
                ["value"]=> string(31) "anastasis_bla@hotmail.com" 
                ["editedBy"]=> string(5) "OWNER" 
                ["flags"]=> string(3) "MSN" 
            } 
            [1]=> object(stdClass)#16 (7) { 
                ["created"]=> string(20) "2007-10-27T04:54:45Z" 
                ["updated"]=> string(20) "2007-10-27T04:54:45Z" 
                ["uri"]=> string(82) "http://social.yahooapis.com/v1/user/SAFWRTHXMZPTISKTIEJPYKLW4I/contact/57/name/1310" 
                ["id"]=> string(3) "130" 
                ["type"]=> string(4) "name" 
                ["value"]=> object(stdClass)#17 (7) {
                    ["givenName"]=> string(61) "- miss elaine" 
                    ["middleName"]=> NULL 
                    ["familyName"]=> NULL 
                    ["prefix"]=> NULL 
                    ["suffix"]=> NULL 
                    ["givenNameSound"]=> NULL 
                    ["familyNameSound"]=> NULL 
                } 
                ["editedBy"]=> string(5) "OWNER" 
            } 
        } 
    } [1]=> object(stdClass)#18 (6) { 
        ["created"]=> string(20) "2009-04-05T13:02:53Z" 
        ["updated"]=> string(20) "2009-04-05T13:02:53Z" 
        ["uri"]=> string(74) "http://social.yahooapis.com/v1/user/SAFWRTHXMZPTISKTIEJPYKLW4I/contact/11" 
        ["isConnection"]=> string(5) "false" 
        ["id"]=> string(3) "116" 
        ["fields"]=> array(2) {
            [0]=> object(stdClass)#19 (8) { 
                ["created"]=> string(20) "2009-04-05T13:02:53Z" 
                ["updated"]=> string(20) "2009-04-05T13:02:53Z" 
                ["uri"]=> string(86) "http://social.yahooapis.com/v1/user/SAFWRTHXMZPTISKTIEJPYKLW4I/contact/116/otherid/52" 
                ["id"]=> string(3) "252" 
                ["type"]=> string(7) "otherid" 
                ["value"]=> string(25) "kellying@hotmail.com" 
                ["editedBy"]=> string(5) "OWNER" 
                ["flags"]=> string(3) "MSN" 
            } [1]=> object(stdClass)#20 (7) { 
                ["created"]=> string(20) "2009-04-05T13:02:53Z" 
                ["updated"]=> string(20) "2009-04-05T13:02:53Z" 
                ["uri"]=> string(83) "http://social.yahooapis.com/v1/user/SAFWRTHXMZPTISKTIEJPYKLW4I/contact/116/name/21" 
                ["id"]=> string(3) "251" 
                ["type"]=> string(4) "name" 
                ["value"]=> object(stdClass)#21 (7) {
                    ["givenName"]=> string(26) "Kelly" 
                    ["middleName"]=> NULL 
                    ["familyName"]=> NULL 
                    ["prefix"]=> NULL 
                    ["suffix"]=> NULL 
                    ["givenNameSound"]=> NULL 
                    ["familyNameSound"]=> NULL } 
                    ["editedBy"]=> string(5) "OWNER" 
            } 
        } 
    } 

【问题讨论】:

  • 你目前有什么代码?
  • 请阅读PHP Manual中关于对象和数组的章节。

标签: php


【解决方案1】:

您需要阅读 var dump types 以确定如何访问数据结构。如果它显示object(例如,在您的转储中它首先列出object(stdClass)#13),那么您将使用-> 运算符来访问列出的元素(例如,$object->contact)。如果它显示array,您可以使用索引符号[0],或者,如果有多个元素,则使用循环构造进行迭代:

foreach ($object->contact as $contact) {
    foreach ($contact->fields as $field) {
        if ($field->type == 'name') {
            echo $field->value->givenName, ' ', echo $field->value->middleName;
        }
    }
}

【讨论】:

    【解决方案2】:

    要访问您将使用的第一个(未经测试):

    $myObject->contact[0]->fields[1]['value']['middleName'];
    

    【讨论】:

      【解决方案3】:

      你应该像这样使用两个嵌套的 foreach 循环和property_exists()

      foreach( $obj->contact as $contact){
        foreach( $contact->fields as $field){
          if( property_exists( $field, 'givenName') &&
             property_exists( $field, 'middleName')){
      
             // Or use if( $field->type == 'name') as webbiedave suggested
      
             // Here goes your desired values
             // Do whatever you want with them
             $field->givenName;
             $field->middleName;
      
             // If you want only one set of names per each contact, add break here
          }
        }
      
        // Here goes handling for contacts wit no desired information
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-08-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-04-02
        • 2014-01-24
        相关资源
        最近更新 更多