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]);
}
11
  • $servers[$id] doesn't exist in you json. Where is the 'id' key in the json? Commented Jan 11, 2017 at 15:54
  • 2
    Instead of type casting just pass TRUE as second parameter to json_decode() Commented Jan 11, 2017 at 15:55
  • $id is the key. @bos570 Commented Jan 11, 2017 at 15:55
  • Thanks @Rizier123 Commented Jan 11, 2017 at 15:56
  • Yes, you're using it as a key but where are you setting value for $id? Commented Jan 11, 2017 at 15:59

2 Answers 2

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.

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

7 Comments

Why print over echo?
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.
FYI: The foreach loop was just to demonstrate my point and wouldn't be used in production.
@SamMearns it doesn't change the fact your code was incorrect.
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.
|
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."

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.