2

I have an array of 'servers' that I'm storing in a JSON file.

The JSON file looks like this: {"1":{"available":1,"players":0,"maxplayers":4}}

I retrieve this value with this:

$servers = (array) json_decode(file_get_contents("activeservers.json"));

However, when I try to access the array with $server = $servers[$id], $server is null. I noticed that the key is in quotes, so I also tried casting $id to a string, and surrounding it with quotes (") which didn't work.

Something to note, is that this code is returning "NULL":

foreach(array_keys($servers) as $key){
    var_dump($servers[$key]);
}
NBTX
  • 581
  • 1
  • 8
  • 24

2 Answers2

2

Your code is wrong. Also you don't need to type cast when doing a json_decode, you can instead set the second parameter to true more info here. Also you don't need to use the array_keys function in your foreach loop, try this.

$json    = '{"1":{"available":1,"players":0,"maxplayers":4}}';
$servers = json_decode($json, true);

foreach($servers as $key => $value) {
    print $value["available"];
}

Do a print_r($value) to get all the array keys available to use. Also you could take advantage of the $key variable to print out the array key of the parent array.

André Ferraz
  • 1,511
  • 11
  • 29
  • Why print over echo? – NBTX Jan 11 '17 at 16:01
  • Its just a matter of preference. The differences are small: echo has no return value while print has a return value of 1 so it can be used in expressions. – André Ferraz Jan 11 '17 at 16:03
  • FYI: The foreach loop was just to demonstrate my point and wouldn't be used in production. – NBTX Jan 11 '17 at 16:29
  • @SamMearns it doesn't change the fact your code was incorrect. – André Ferraz Jan 11 '17 at 16:32
  • Only the lack of the `TRUE` parameter? – NBTX Jan 11 '17 at 16:33
  • No, you were using array_keys on the foreach loop when it is pointless. – André Ferraz Jan 11 '17 at 16:37
  • 1
    Yes I understand it but it is still pointless using it. Because the `var_dump($servers[$key]);` you're referencing is not the `array_keys($servers)` in your foreach loop. To prove my point is you do this `foreach($test_servers = array_keys($servers) as $value) { print $test_servers[$value]; }` your code will break. Am on this because I don't think you understood the use of array_keys, otherwise you would not use it even if it was for demonstration purposes. Am not picking on you by the way. – André Ferraz Jan 11 '17 at 17:01
1

Thanks, @Rizier123 (who solved the question).

Apparently passing TRUE as the second parameter to my json_decode function fixes the issue.

After checking the PHP documentation for json_decode() (PHP: json_decode), it seems that passing this parameter means that the resulting decoded array is automatically converted into an associative array (and this is recurring, meaning that this automatically happens for sub-arrays).

Edit: @Rizier123 also says that "you might want to read: stackoverflow.com/a/10333200 to understand a bit better why it is so "weird" and your method didn't work properly."

NBTX
  • 581
  • 1
  • 8
  • 24