简体   繁体   中英

how to echo string from api json response with multi dimensional array

I have a php script thats connected to an API and Im trying to format the response to look nice on the page. However I cant seem to access the data I need.

The json response looks something like this ---

[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
                                    ................

I have tried the following --

//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;

After I figure that, I am trying to loop the results in a for each loop -

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

What am I doing wrong?

You have not name but key and value so for json_decode as associative array

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

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

But inside of your metadata you have another array that you should iterate then echo only value

foreach($items as $item) {

$secondLoop=item->metadata;

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

Ok (update answer) I try to make object array like you to test code

<?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;
 }
}

And the answer is

    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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM