1

I was wondering if someone could help please. I have a JSON feed from an API, and I can never figure out the levels to get the information I need?

the JSON is below

{
  "sourceId": "1234",
  "sourceType": "MONITOR",
  "searchMeta": {
    "startTimestamp": 1462361513,
    "endTimestamp": 1462508560,
    "maxResults": 10000,
    "metricType": [
      "clients"
    ],
    "family": [
      "Product"
    ],
    "metricsInterval": "M5"
  },
  "clients": {
    "One": [
      {
        "timestamp": 1462361400,
        "avg": 2,
        "min": 1,
        "max": 3,
        "probes": 6,
        "samples": 3,
        "sources": [
          "123",
          "234",
          "345",
          "456",
          "567",
          "789"
        ]
      },

I was wanting to get the Probes value and the Samples value into a variable

foreach($json['clients'] as $range => $product){

echo $product['timestamp']." Probes: ".$product['probes']." Samples: ".$product['samples']." <br>";

}

Thanks in advance

2
  • $product[...] instead of $range? Commented May 6, 2016 at 5:32
  • Thanks, Sorry, I changed one and not the others. It will still not print the values though. I edited the main to correct the error Commented May 6, 2016 at 5:35

3 Answers 3

1

I assume you'll have a range of clients (hence the loop). So you'll need to loop each client too as it's an array of object's.

If you visualise it, that looks like this:

CLIENTS = ARRAY(
    1 => ARRAY( OBJECT{} ), // you want the OBJECT{}
    ....etc
)

So the below loop will get it for you.

$data = json_decode($json, TRUE);

foreach($data['clients'] as $range => $product) {
    foreach($product as $element){
        echo $element['timestamp']." Probes: ".$element['probes']." Samples: ".$element['samples']." <br>";    
    }
}

Example/Demo

Returns the following:

1462361400 Probes: 6 Samples: 3 <br>
Sign up to request clarification or add additional context in comments.

2 Comments

Outstanding Darren. Thank you so much for this. and the explanation. A true legend!!!!!
My pleasure @RenegadeRob - just a lil tip for you too while dealing with json; check out jsoneditoronline.com - it'll help you understand how to loop and the structure and all! ;-)
0

Try this.

$data = json_decode($json,true);
foreach($data['clients'] as $client){

echo $client['timestamp']." Probes: ".$client['probes']." Samples: ".$client['samples']." <br>";

}

Comments

0

The demand of OP:

I was wanting to get the Probes value and the Samples value into a variable

Simple just name your json file as $json.

$arr = json_decode($json);

echo $samples = $arr->clients->One[0]->samples; //3
echo $probes = $arr->clients->One[0]->probes; //6

2 Comments

There are many more, so I really would need it in a foreach though. About 300 fields
you are asking to get only two values. so i did.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.