dongxian7489 2019-07-13 19:28
浏览 161

如何使用动态更改的键值访问嵌套的JSON对象

I'm trying to get JSON data into my PHP script and the problem is that this is a nested JSON object with dynamically changed key value (I've converted JSON objects into PHP objects):

stdClass Object
(
    [665261] => stdClass Object
        (
            [id] => 665261
            [SpeiseplanName] => Campus Mensa Wismar
            [Datum] => 2019-07-12
            [KstNr] => 462
            [ArtikelText] => ein Brathering ohne Mittelgräte
            [ZusatzStoffe] => 9 Fi Gl
            [ZusatzStoffeText] => mit Süßungsmittel, Fische, Gluten
            [PeStud] => 0.75
            [PeBed] => 1.35
            [PeGast] => 1.75
            [sortierung] => 46
        )

    [665262] => stdClass Object
        (
            [id] => 665262
            [SpeiseplanName] => Campus Mensa Wismar
            [Datum] => 2019-07-12
            [KstNr] => 462
            [ArtikelText] => zwei Bratheringe ohne Mittelgräte
            [ZusatzStoffe] => 9 Fi Gl
            [ZusatzStoffeText] => mit Süßungsmittel, Fische, Gluten
            [PeStud] => 1.25
            [PeBed] => 1.9
            [PeGast] => 2.25
            [sortierung] => 47
        )

    [665263] and so on.

So, I've already get the data like this:

<p id="desc"><?= htmlReady(_($data[665261]['ArtikelText'])) ?></p>

The output was then "zwei Bratheringe ohne Mittelgräte" as expected. But this numeric key 665261 is dynamic and changed every day.

So how can I access date with key's values like this? Thank you for your help.

  • 写回答

2条回答 默认 最新

  • dtrovwl75780 2019-07-13 20:07
    关注

    You just need to use foreach loop, like this:

    Suppose your object of objects name is $objects:

    foreach ($objects as $obj) {
        echo '<p id="desc">' . htmlReady(_($obj->ArtikelText)) . '</p>';
    }
    

    Or if you have a array of arrays, with the name of $arrays:

    foreach ($arrays as $arr) {
        echo '<p id="desc">' . htmlReady(_($arr['ArtikelText'])) . '</p>';
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题