Jasper
Jasper

Reputation: 137

getting element names and values of json respsonse php

I would like to get all the elements and their values from the json response. I have the following response (snippet, it has more elements):

stdClass Object ( [Count] => 15244 [Warnings] => Array ( ) [Machines] => Array ( [0] => stdClass Object ( [Id] => 23 [Modified] => 2019-09-18 06:38:04 [Created] => 2016-03-10 14:11:39 ) [1] => stdClass Object ( [Id] => 51 [Modified] => 2019-09-18 08:15:52 [Created] => 2016-06-15 09:13:16 )))

Now I would like to get the results something like:

ID: 23, Modified: 2019-09-18 06:38:04, Created: 2016-03-10 14:11:39
ID: 51, Modified: 2019-09-18 08:15:52, Created: 2016-06-15 09:13:16

The problem is, that I don't want to hard-code the element names like "ID", "Created" and so on, because the complete array per Machines has about 50 elements.

This is what I tried:

$obj = json_decode($body);

foreach ($obj->Machines as $comp) {
    $sup =key($comp);
    echo key($comp)."-".$comp->$sup."<br>";
}

But this only gives the output:

Id-23
Id-51

So I only get the first KEY showed. I don't know how to get to the next element like "Modified" in the loop.

Thanks for the support!

Upvotes: 1

Views: 56

Answers (4)

Thrallix
Thrallix

Reputation: 731

What you did is correct, though this is a multi-dimensional array.

You need several foreach loops to iterate to the dimension you want.

$response = [];

foreach($obj->Machines as $comp) {
    foreach($comp as $key => $value) {
        $response[$key] = '';
        foreach($value as $title => $display) {
            $response[$key] .= $title . ': ' . $display . ', ';
        }
        $response[$key] = rtrim($response[$key], ', ');
    }
}   

var_dump($response);

Upvotes: 0

Jasper
Jasper

Reputation: 137

This is what did the trick now:

$obj = json_decode($body);
print_r($obj);

//echo $obj->Machines[0]->Id;
foreach ($obj->Machines as $comp) {
        echo "<BR>";
    foreach($comp as $key => $value){
        echo $key.":".$value." - ";
    }
}

Gives the output like:

Id:148 - Modified:2019-09-18 07:16:47 - Created:2016-11-08 08:21:36
Id:143 - Modified:2019-09-15 04:13:21 - Created:2016-11-04 05:34:01

Again, really great support again! thank you very much!!

Upvotes: 0

Rahul
Rahul

Reputation: 18557

You can use array map to echo the same,

foreach ($obj->Machines as $comp) {
    echo implode(', ', array_map(function ($val, $key) {
              return sprintf("%s:'%s'", $key, $val);
        }, $comp, array_keys($comp)))."<br/>";
}

Solution 2:-

foreach ($obj->Machines as $comp) {
    echo str_replace('=',':',http_build_query($comp,'',', '));
}

http_build_query — Generate URL-encoded query string

Upvotes: 2

MH2K9
MH2K9

Reputation: 12039

Convert your JSON data to array using json_decode(). Make an iteration over the array using array_map(), again make another nested iteration using array_walk() to replace the value to key:value pear format. Finally join the converted array to string by the glue of comma.

Code example:

$response = json_decode($response, true);
$result = array_map(function ($val) {
    array_walk($val, function (&$v, $k) { $v = "$v: $k"; });
    return implode(',', $val);
}, $response);
print_r($result);

Upvotes: 1

Related Questions