【问题标题】:Decode multidimensional json arrays using php使用php解码多维json数组
【发布时间】:2018-06-15 09:39:00
【问题描述】:

我的 json 结构如下:

[46] => stdClass Object
    (
        [task] => stdClass Object
            (
                [id] => b3:1130806504
                [name] => Prezentare Logos
                [type] => task
                [status] => closed
                [iteration] => Creatie si strategie
                [number] => 
                [dueAt] => 
            )

        [project] => stdClass Object
            (
                [id] => b3:7700161
                [name] => F.36.35.23.5 Ibalact/Mirdatod
                [workspace] => G7
            )

        [time] => stdClass Object
            (
                [total] => 10800
                [users] => stdClass Object
                    (
                        [54928] => 10800
                    )

            )

        [estimate] => 
    )

[47] => stdClass Object
    (
        [task] => stdClass Object
            (
                [id] => b3:1131005900
                [name] => Declinari KV
                [type] => task
                [status] => open
                [iteration] => Creatie
                [number] => 
                [dueAt] => 
            )

        [project] => stdClass Object
            (
                [id] => b3:8035237
                [name] => F.52.49.27.4 Single Serve
                [workspace] => G7
            )

        [time] => stdClass Object
            (
                [total] => 28800
                [users] => stdClass Object
                    (
                        [57889] => 28800
                    )

            )

        [estimate] => 
    )

如何回显每个项目的名称和每个用户的时间:例如:[57889] => 28800? 这是我迄今为止尝试过的:

$decoded = json_decode($result,true);

echo "<pre>";
print_r($decoded);

//echo $decoded[0]->name;

foreach($decoded as $ar){

    foreach($ar->project as $users){

   echo $users;
   echo "<br>";

    }
}

这给了我以下信息:

 [id] => b3:7700161
 [name] => F.36.35.23.5 Ibalact/Mirdatod
 [workspace] => G7

如何只回显每个项目的名称?我做了一个额外的 foreach,我什至做了以下参考: echo $users->name 但它什么也没显示。我错过了一些东西,但我无法弄清楚是什么。任何帮助。

【问题讨论】:

  • 首先,您应该向json_decode() 提供第二个参数。将您的行更改为$decoded = json_decode($result,true);,您将得到一个数组而不是stdObject。

标签: php arrays json multidimensional-array


【解决方案1】:

试试下面的

foreach($arr as $ar){
    foreach($ar->users as $users){
        echo $ar->name. ' is '.$users;
    }
}

【讨论】:

  • 它什么也没给。
  • var_export 数组并将其发布在问题中
  • 它工作正常,我已将第二个参数设置为 true:json_decode($result,true);我的错。删除 true 后,一切正常。感谢您的解决方案。
  • 上面的代码给了我以下信息:[id] => b3:8035237 [name] => F.52.49.27.4 Single Serve [workspace] => G7 问题:我在这个中做了另一个 foreach 2 foreach 来获取项目的名称,但它似乎不起作用,如下所示: foreach($decoded as $ar){ foreach($ar->project as $users){ foreach($users->name as $bla){ 回声 $bla; } } } 但它什么也没显示..有什么想法吗?
  • 您编辑过您的问题吗? .我已经回答了你之前的问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-03-18
  • 1970-01-01
  • 2021-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-25
相关资源
最近更新 更多