如何使用多维数组从api json响应中回显字符串

时间:2019-05-18 07:58:25

标签: php json api multidimensional-array

我有一个连接到API的php脚本,我试图格式化响应以使其在页面上看起来不错。但是我似乎无法访问所需的数据。

json响应看起来像这样---

[body] => stdClass Object
        (
            [items] => Array
                (
                    [0] => stdClass Object
                        (
                            [metadata] => Array
                                (
                                    [0] => stdClass Object
                                        (
                                            [key] => type
                                            [value] => challenge
                                        )

                                    [1] => stdClass Object
                                        (
                                            [key] => name
                                            [value] => do somethhing
                                        )

                                    [2] => stdClass Object
                                           .................
                                         )
                                      )
                     [1] => stdClass Object
                        (
                            [metadata] => Array
                                (
                                    [0] => stdClass Object
                                        (
                                            [key] => type
                                            [value] => challenge
                                        )

                                    [1] => stdClass Object
                                        (
                                            [key] => name
                                            [value] => do somethhing else
                                        )

                                    [2] => stdClass Object
                                    ................

我尝试了以下方法-

//echo $response->raw_body->metadata->name;
//echo $response->raw_body->metadata;
//echo $response->raw_body;

$newresponse = $response->raw_body;
$items = json_decode($newresponse, true);

$response1 = json_decode($response, true);
echo $response1['body']['items']['metadata']['name'];

echo $items[2]['name'];
//echo $items->items;
//echo $items->items->metadata->name;
//echo $items->metadata->name;
echo $items->items[1]->metadata->name;

我弄清楚了之后,我尝试将结果循环到每个循环中-

foreach($items as $item) {
echo '<div>';
echo $item->items->metadata->name;
echo '</div>';
}

我在做什么错了?

2 个答案:

答案 0 :(得分:0)

您没有名称而是键和值,因此对于json_decode作为关联数组

echo ['body']['items'][0]['metadata'][0]['key'];  // type

echo ['body']['items'][0]['metadata'][0]['value'];  // challenge

答案 1 :(得分:0)

但是在元数据中,您有另一个数组应该迭代然后仅回显值

foreach($items as $item) {

$secondLoop=item->metadata;

foreach ($secondLoop as $loop)
{
    if($loop->key=="name")
        echo $loop->value;
 }
}

好(更新答案),我尝试使像您这样的对象数组测试代码

<?php

$c['body'] = (object)['items'=>[(object)["metadata"=>[(object)['key'=>'name','value'=>'cc']]]]];

var_dump($c);

$items=$c['body']->items;

foreach($items as $item) {

$secondLoop=$item->metadata;

foreach ($secondLoop as $loop)
{
    if($loop->key=="name")
        echo $loop->value;
 }
}

答案是

    array(1) {
  ["body"]=>
  object(stdClass)#3 (1) {
    ["items"]=>
    array(1) {
      [0]=>
      object(stdClass)#2 (1) {
        ["metadata"]=>
        array(1) {
          [0]=>
          object(stdClass)#1 (2) {
            ["key"]=>
            string(4) "name"
            ["value"]=>
            string(2) "cc"
          }
        }
      }
    }
  }
}
cc