1

I want to echo an array data from json decode result, so I have tried both the file_get_contents and also curl which works.

This is the response I get from the server which is json output..

{"Servers": {
 "lastChangedDate": null,
 "ServersList":  [
    {
   "online": "The server is UP",
   "offline": "The server is DOWN",
   "maintainace": "The server is currently in maintenance mode",
   "location": "EU",
   "ip": "x.x.x.x"
  },
    {
   "online": "The server is UP",
   "offline": "The server is DOWN",
   "maintainace": "The server is currently in maintenance mode",
   "location": "US",
   "ip": "x.x.x.x"
  }
 ]
}}

now then the output will be an array like this after decoding it..

Array (
    [Servers] => Array (
        [lastChangedDate] =>
        [ServersList] => Array (
            [0] => Array (
                [online] => The server is UP
                [offline] => The server is down
                [maintenance] => The server is currently in maintenance mode
                [location] => EU
                [ip] => x.x.x.x
            )
            [1] => Array (
                [online] => The server is UP
                [offline] => The server is DOWN
                [maintenance] => The server is currently in maintenance mode
                [location] => US
                [ip] => x.x.x.x
            )
        )
    )
)

Here is my php code

<?php 
    $request = file_get_contents("test.json");
    $input = json_decode($request, true);
    echo $input['Servers']['lastChangedDate']['ServersList'][0]['online'];
?>

demo with print_r ($input); instead of echo http://phpad.org/run/1666334020

So in my main page I want to output to be like this http://codepen.io/anon/pen/jEEPMG.html

3
  • 2
    $input['Servers']['ServersList'][0]['online']; should work Commented Nov 18, 2014 at 15:19
  • that works man.. thank you.. you could've posted as answer so I could mark it as an answer. Commented Nov 18, 2014 at 15:23
  • print_r output is indented if you display it properly. just follow the indentation down the line. Commented Nov 18, 2014 at 15:34

2 Answers 2

1

Entries $input['Servers']; and $input['lastChangedDate']; are on the same level in the array, so you can't access$input['Servers']['lastChangedDate'].

I think you're trying to do:

$input['Servers']['ServersList'][0]['online'];
Sign up to request clarification or add additional context in comments.

Comments

1

in the json you posted above 'lastChangedDate' is null means you can't access it with

$input['Servers']['lastChangedDate']['ServersList'][0]['online'];

First you should find why lastChangedDate is null. $input['Servers']['lastChangedDate']['ServersList'][0]['online'];

The bold text is where the access conflict begins. PHP should also output you an error like "Undefined index: ServerList" so you need to first fill lastChangedDate to make further requests to its content

Could it be you wanted to access

$input['Servers']['ServersList'][x]['online']; 

3 Comments

he shouldn't traverse this way and has nothing to do with NULL, because the structure is like this: Servers->ServersList-> etc.
he is calling lastChangedDate in his example which is NULL, so i assume it was just a mistake he called lastChangedDate
I think it was null for the last years or so, I don't really know why though.. since I don't have a contact with those developers.. im new to php so I didn't really knew whether to call it or not.

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.