0

I tried to work with basic, few param. JSON code and there wasn't any problem with that.

BUT now i need work with this "advanced" JSON and I am kinda lost

  {
   "code":"success",
   "username":"x",
   "nodes":[
      {
         "id":"68",
         "time":987
      },
      {
         "id":"69",
         "time":987
      }
   ]
}

When i tried to get the values into PHP variable with previou code, I wasn't able to get ID and TIME, CODE and SUCCESS isn't problem.

PHP code I used:

$url = "example.com";
$url = str_replace(" ","%20",$url);
$json = @file_get_contents($url);
$details = json_decode($json, TRUE);
// print_r($details);
echo $details[code];

Thank you guys!

3
  • Does this answer your question? How to access JSON decoded array in PHP Commented Nov 10, 2021 at 3:27
  • echo $details['nodes'][0]['id']; Commented Nov 10, 2021 at 3:28
  • Thank you! It is working. May I ask you, how can I count and print out number of total arrays? "nodes":[ { "id":"68", "time":987 }, { "id":"69", "time":987 } ] in this case i would like to get number 2 Commented Nov 10, 2021 at 3:52

1 Answer 1

0
<?php
$url = "example.com";
$url = str_replace(" ","%20",$url);
$json = @file_get_contents($url);
$details = json_decode($json, TRUE);
if (is_array($details['nodes'])) {
    echo "node count: " . count($details['nodes']) . "<br />";
    foreach ($details['nodes'] as $node) {
        echo "id: " . $node['id'] . "<br />";
        echo "time: " . $node['time'] . "<br />";
    }
}

Sign up to request clarification or add additional context in comments.

Comments

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.