duan19913 2018-07-17 10:30
浏览 157
已采纳

php从字段名称数组中获取对象数据

I have a result set from a query that gives me an object that has dozens of fields, the below example is a subset:

[79] => stdClass Object
    (
        [name] => John Doe
        [email] => john@doe.com
        [ext] => 4004
        [options] => stdClass Object
            (
                [type] => friend
                [rating] => Excellent
                [context] => default
            )
        [address] => 123 Anywhere St
    )

Instead of plowing through each field, because I only want a handful of them, I am trying to use an array to get what i want:

$fields = array('name','email','options->type','options->rating','address');

so then i do:

foreach ($result as $k => $v){
    foreach ($fields as $kk => $vv){
        echo $kk. " - " . $v->$kk."<br>";
    }
}

Which gives me field name and its value.

name - John Doe
email - john@doe.com
address - 123 Anywhere St.

However, anything in that sub object(options) is giving me a blank result.

  • 写回答

5条回答 默认 最新

  • duangutian1426 2018-07-17 10:49
    关注

    You can use a recursive function providing that you don't mind changing the format of your $fields var to include arrays. In my opinion this makes it easier to read anyway, and easier to handle in code.

    The benefit of using a recursive function is that it will handle any depth.

    $o = (object) [
        'name' => 'John Doe',
        'email' => 'john@doe.com',
        'ext' => 4004,
        'options' => (object) [
            'type' => 'friend',
            'rating' => 'Excellent',
            'context' => 'default',
            'any' => (object) [
                'depth' => (object) [
                    'will' => 'work'
                ]
            ]
        ],
        'address' => '123 Anywhere St'
    ];
    $fields = [
        'name',
        'email',
        'options' => [
            'type',
            'rating',
            'any' => [
                'depth' => [
                    'will'
                ]
            ]
        ],
        'address'
    ];
    
    function getObjectProps($o, $fields, $parent = '') {
        if (strlen($parent)) {
            $parent .= '->';
        }
        foreach ($fields as $k => $v) {
            if (is_array($v)) {
                getObjectProps($o->{$k}, $v, $parent . $k);
            } else {
                echo $parent . $v . ' - ' . $o->{$v} . '<br/>';
            }
        }
    }
    getObjectProps($o, $fields);
    

    Output:

    name - John Doe
    email - john@doe.com
    options->type - friend
    options->rating - Excellent
    options->any->depth->will - work
    address - 123 Anywhere St
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动