duanlian1978 2018-09-18 15:48
浏览 584
已采纳

php - 如何从数组对象中获取值

I use php code and I have a problem with getting value of an array object. When I use var_dump($arr), I get this (the code below is formatted for better readability):

object(League\OAuth2\Client\Provider\GoogleUser)#5 (1) {
    ["response":protected]=> array(5) {
        ["emails"]=> array(1) {
            [0]=> array(1) {
                ["value"]=> string(21) "thienlam129@gmail.com"
            }
        }
        ["id"]=> string(21) "115281634466837725533"
        ["displayName"]=> string(18) "thiên lâm trần"
        ["name"]=> array(2) {
            ["familyName"]=> string(6) "trần"
            ["givenName"]=> string(11) "thiên lâm"
        }
        ["image"]=> array(1) {
            ["url"]=> string(98) "https://lh3.googleusercontent.com/-SpWfKGTcQt8/AAAAAAAAAAI/AAAAAAAAAB8/IlGQQnvy7so/photo.jpg?sz=50"
        }
    }
}

Please, tell me how to get emails value.

  • 写回答

2条回答 默认 最新

  • dongmu3187 2018-09-18 18:56
    关注

    You could cast the object as an array as such:

    var_dump((array) $object);
    

    Or

    $array = (array) $object;
    

    And access all its properties but you might have to array walk and filter the name of the class out.

    But this is a hack and an example of bad practise. Accessing protected properties by its methods is the way to go or else request json object instead, if possible.

    Naturally, you would say

    $object = (object) $inputObject;
    $value = (string) $object->getValue();
    

    Or

    $value = (array) $object->getArray();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵