duanbinren8906 2016-12-23 11:29
浏览 33
已采纳

将嵌套的stdClass插入数据库

I have been working on a project to automatic fetch information from a client page. Its basically a function that call a bunch of other functions and arrange the information in an stdClass Object. I thought I was clever and made it "searchable" but I have encountered some small problems.

Here is an example of the output:

stdClass Object
(
    [ud] => stdClass Object
        (
            [name] => stdClass Object
                (
                    [svenska] => Uddev
                    [engelska] => uddev
                )

            [biografer] => stdClass Object
                (
                    [0] => stdClass Object
                        (
                            [namn] => Drottning. Uddev
                            [id] => 196
                            [salonger] => stdClass Object
                                (
                                    [0] => stdClass Object
                                        (
                                            [salong] => Vag 1
                                            [platser] => 100 platser
                                            [ljud] => SOU
                                        )

                                    [1] => stdClass Object
                                        (
                                            [salong] => Vag 2
                                            [platser] => 100 platser
                                            [ljud] => SOU
                                        )

                                    [2] => stdClass Object
                                        (
                                            [salong] => Vag 3
                                            [platser] => 60 platser
                                            [ljud] => SOU
                                        )

                                    [3] => stdClass Object
                                        (
                                            [salong] => Vag 4
                                            [platser] => 50 platser
                                            [ljud] => SOU
                                        )

                                    [4] => stdClass Object
                                        (
                                            [salong] => Vag 5
                                            [platser] => 50 platser
                                            [ljud] => DOLBY
                                        )

                                    [5] => stdClass Object
                                        (
                                            [salong] => Vag 6
                                            [platser] => 34 platser
                                            [ljud] => 
                                        )

                                )

                        )

                )

        )
    [ma] => stdClass Object
        (
...... And the following object

As you can see, the index (or is it the key) for the first object is ud.

I thought that i could do something like this:

$test = $crawler->fetchAllArray();

foreach ($test as $key => $value) {
    if($key == 'ud'){
        foreach ($value->{ud} as $child => $ud) { # line 295
            echo "id: $key" . ", name: $ud->name->svenska";
        }
    }
}

And that would generate id: ud, name: Uddev, but I receive the following error:

Notice: Use of undefined constant ud - assumed 'ud' in test.php on line 295

Notice: Undefined property: stdClass::$ud in test.php on line 295

Warning: Invalid argument supplied for foreach() in test.php on line 295

How do I properly echo the information from a specific object?

  • 写回答

1条回答 默认 最新

  • douchanxiu5636 2016-12-23 11:46
    关注

    Why do you have this?

    $value->{ud}
    

    You should be accessing the nested object as:

    $value
    

    or even as

    $test->ud
    

    directly.

    And what's the second foreach for? Not particularly clear what you want to accomplish, but you could do:

    foreach ($test as $key => $value) {
        if($key == 'ud'){
              echo "id: $key" . ", name: ".  $value->name->svenska;
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器