doutao6653 2012-02-09 22:51
浏览 82
已采纳

用PHP读取var_dump数据

May I know how can I retrieve givenNmae and middlename value in each array in the following example with PHP? Thank you.

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" 
            } 
        } 
    } 
  • 写回答

3条回答 默认 最新

  • doumi1852 2012-02-09 22:57
    关注

    You'll want to read the var dump types to determine how to access the data structure. If it says object (e.g., in your dump it first lists object(stdClass)#13), then you'll use -> operator to access the listed elements (e.g., $object->contact). If it says array, you can use index notation [0] or, if more than one element, iterate with a loop construct:

    foreach ($object->contact as $contact) {
        foreach ($contact->fields as $field) {
            if ($field->type == 'name') {
                echo $field->value->givenName, ' ', echo $field->value->middleName;
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?