This appears to work:
$url = 'http://www.worldweatheronline.com/feed/weather.ashx?q=schruns,austria&format=json&num_of_days=5&key=8f2d1ea151085304102710%22';
$content = file_get_contents(
json = json_decode($content, true);
foreach($json['data']['weather'] as $item) {
print $item['date'];
print ' - ';
print $item['weatherDesc'][0]['value'];
print ' - ';
print '<img src="' . $item['weatherIconUrl'][0]['value'] . '" border="0" alt="" />';
print '<br>';
}
If you set the second parameter of json_decode to true, you get an array, so you cant use the -> syntax. I would also suggest you install the JSONview Firefox extension, so you can view generated json documents in a nice formatted tree view similiar to how Firefox displays XML structures. This makes things a lot easier.
Answer from BMBM on Stack OverflowWhy does json_decode return null?
How do I convert JSON to an array in PHP?
How do I read a JSON file in PHP?
This appears to work:
$url = 'http://www.worldweatheronline.com/feed/weather.ashx?q=schruns,austria&format=json&num_of_days=5&key=8f2d1ea151085304102710%22';
$content = file_get_contents(
json = json_decode($content, true);
foreach($json['data']['weather'] as $item) {
print $item['date'];
print ' - ';
print $item['weatherDesc'][0]['value'];
print ' - ';
print '<img src="' . $item['weatherIconUrl'][0]['value'] . '" border="0" alt="" />';
print '<br>';
}
If you set the second parameter of json_decode to true, you get an array, so you cant use the -> syntax. I would also suggest you install the JSONview Firefox extension, so you can view generated json documents in a nice formatted tree view similiar to how Firefox displays XML structures. This makes things a lot easier.
If you use the following instead:
$json = file_get_contents(
data = json_decode($json, TRUE);
The TRUE returns an array instead of an object.
As per the documentation, you need to specify true as the second argument if you want an associative array instead of an object from json_decode. This would be the code:
$result = json_decode($jsondata, true);
If you want integer keys instead of whatever the property names are:
$result = array_values(json_decode($jsondata, true));
However, with your current decode you just access it as an object:
print_r($obj->Result);
try this
$json_string = 'http://www.domain.com/jsondata.json';
$jsondata = file_get_contents($json_string);
$obj = json_decode($jsondata,true);
echo "<pre>";
print_r($obj);
I have a json_files.php file where I'm placing several JSON objects. One of them is the following:
$jsonGreetingsListEnglish = '{
"1": "Hello",
"2": "Hi there",
"3": "Morning",
"4": "Evening",
"5": "Hey",
"6": "Howdy",
"7": "Hiya",
"8": "Welcome",
"9": "Hi",
"10": "Yo!",
"11": "Sup?"
"12": "Nice"
"13": "What up?"
"14": "Aloha"
"15": "Oi!",
"16": "Ahoy!",
"17": "Cheers"
"18": "Alright?",
"19": "Hey yo!",
"20": "Good day"
}';On a second file I'm decoding it:
include_once "json_files.php"; $greetingsListEnglish = json_decode($jsonGreetingsListEnglish, true);
Later in the same (second) file, I declared this function:
/* Chooses random greeting and encodes it
Retains unencoded greeting for output */
function generateEnglishGreeting() {
global $greetingsListEnglish;
$index = array_rand($greetingsListEnglish);
$unencodedGreeting = $greetingsListEnglish[$index];
$encodedGreeting = mb_convert_encoding($unencodedGreeting, "UTF-16LE");
return [$unencodedGreeting, $encodedGreeting];
}However, when I called this function I get an error that says the decoded array is empty:
Fatal error: Uncaught TypeError: array_rand(): Argument #1 ($array) must be of type array, null given in C:\xampp\htdocs\join_avenue_visitor_generator\visitor_data_functions.php:88 Stack trace: #0
I ran this to check and when the script runs it always prints "Empty":
if (!empty($greetingsListEnglish)) {
echo "Not empty";
} else {
echo "Empty";
}Am I messing up some syntax or something?
Update: As mentioned in several replies, there were several commas missing. Thanks for all the extra advice.