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 Overflow
🌐
PHP
php.net › manual › en › function.json-decode.php
PHP: json_decode - Manual
If you want to enforce an array to encode to a JSON list (all array keys will be discarded), use: json_encode(array_values($array)); If you want to enforce an array to encode to a JSON object, use: json_encode((object)$array); See also: https://www.php.net/manual/en/function.array-is-list.php ... Warning: As the section "return values" mentions, the return value NULL is ambiguos. To repeat, it can mean three things: * The input string had the value "null" * There was an error while parsing the input data * The encoded data was deeper than the recursion limit To distinguish these cases, json_last_error() can be used.
Discussions

Help parsing JSON array using PHP
Test it first with array_key_exists() More on reddit.com
🌐 r/PHPhelp
4
1
April 20, 2022
How to extract and access data from JSON with PHP? - Stack Overflow
Related: Able to see a variable ... JSON exploration in context of PHP is possible here: array.include-once.org ... Please can I know that why this question not consider as a duplicate question even 9 or less users marked as a duplicate for stackoverflow.com/questions/4343596/parsing-json-file... More on stackoverflow.com
🌐 stackoverflow.com
JSON Parser: parse JSON of any dimension from any source
This is great. What I've used so far is a solution that can treat the first level as an array and read each node separately. This is more generic. More on reddit.com
🌐 r/PHP
6
56
June 16, 2023
Parsing a 200MB JSON file

What you've built here is a streaming parser and there's already a few good implementations for PHP, this one is great and has tests: https://github.com/salsify/jsonstreamingparser

In terms of your project I'd say a few things:

  • No tests!

  • You're not following PSR-2 and instead using Laravel's conventions. Inside Laravel packages thats fine but if you're releasing framework independent code, follow PSR-2.

  • Strange indention.

  • Over use of private instead of protected (and a few instances where public should be protected).

More on reddit.com
🌐 r/PHP
7
13
August 8, 2012
🌐
W3Schools
w3schools.com › js › js_json_php.asp
JSON PHP
Here is a JavaScript on the client, using an AJAX call to request the PHP file from the array example above: Use JSON.parse() to convert the result into a JavaScript array:
🌐
Envato Tuts+
code.tutsplus.com › home › cloud & hosting
How to Parse JSON in PHP | Envato Tuts+
May 31, 2021 - This tutorial will teach you how to read a JSON file and convert it to an array in PHP. Learn how to parse JSON using the json_decode() and json_encode() functions.
🌐
ReqBin
reqbin.com › code › php › ozcpn0mv › php-parse-json-example
How do I parse JSON string in PHP?
July 27, 2023 - To parse a JSON string to a PHP object or array, you can use the json_decode($json) function. The json_decode() function recursively converts the passed JSON string into the corresponding PHP objects.
🌐
Reddit
reddit.com › r/phphelp › help parsing json array using php
r/PHPhelp on Reddit: Help parsing JSON array using PHP
April 20, 2022 -

I have figured out half of what I am trying to do here.

I have a JSON array that is being returned via API. I am wanting to parse that array and pull out just the Unique ID and Email Address for each user.

Each user has a key called 'unique_id' regardless of whether or not it is defined. Each user only has a key called 'email' if a user in fact has an email address defined.

Sample Code:

 foreach ($arr["data"]["users"] as $value) {
   echo $value["unique_id"];
   echo $value["email"]; 
}

This code works....sort of. This will output the unique_id for each user. If there is no value defined, it simply returns a blank value. Cool. However, for the key email, I get an error: Undefined array key "email".

Am I getting this because not every user has this key?

You can see a sample of the array output here: https://pastebin.com/9D4mWyKL

Find elsewhere
🌐
W3Schools
w3schools.com › php › func_json_decode.asp
PHP json_decode() Function
json_decode() json_encode() json_last_error() PHP Keywords
🌐
CodeSignal
codesignal.com › learn › courses › handling-json-files-with-php › lessons › parsing-and-accessing-json-data-with-php
Parsing and Accessing JSON Data with PHP
Decode the data using json_decode() to convert it into a PHP associative array. ... To access specific data from the parsed JSON, such as the school name, you navigate the hierarchical structure using keys:
🌐
GeeksforGeeks
geeksforgeeks.org › php › how-to-parse-a-json-file-in-php
How to parse a JSON File in PHP? - GeeksforGeeks
This function takes one parameter, which is the file path, and returns the content of the file as a string. This string can then be decoded and used in PHP. ... 'my_data.json' is the file name.
Published   June 24, 2025
Top answer
1 of 3
610

Intro

First off you have a string. JSON is not an array, an object, or a data structure. JSON is a text-based serialization format - so a fancy string, but still just a string. Decode it in PHP by using json_decode().

 $data = json_decode($json);

Therein you might find:

  • scalars: strings, ints, floats, and bools
  • nulls (a special type of its own)
  • compound types: objects and arrays.

These are the things that can be encoded in JSON. Or more accurately, these are PHP's versions of the things that can be encoded in JSON.

There's nothing special about them. They are not "JSON objects" or "JSON arrays." You've decoded the JSON - you now have basic everyday PHP types.

Objects will be instances of stdClass, a built-in class which is just a generic thing that's not important here.


Accessing object properties

You access the properties of one of these objects the same way you would for the public non-static properties of any other object, e.g. $object->property.

$json = '
{
    "type": "donut",
    "name": "Cake"
}';

$yummy = json_decode($json);

echo $yummy->type; //donut

Accessing array elements

You access the elements of one of these arrays the same way you would for any other array, e.g. $array[0].

$json = '
[
    "Glazed",
    "Chocolate with Sprinkles",
    "Maple"
]';

$toppings = json_decode($json);

echo $toppings[1]; //Chocolate with Sprinkles

Iterate over it with foreach.

foreach ($toppings as $topping) {
    echo $topping, "\n";
}

Glazed
Chocolate with Sprinkles
Maple

Or mess about with any of the bazillion built-in array functions.


Accessing nested items

The properties of objects and the elements of arrays might be more objects and/or arrays - you can simply continue to access their properties and members as usual, e.g. $object->array[0]->etc.

$json = '
{
    "type": "donut",
    "name": "Cake",
    "toppings": [
        { "id": "5002", "type": "Glazed" },
        { "id": "5006", "type": "Chocolate with Sprinkles" },
        { "id": "5004", "type": "Maple" }
    ]
}';

$yummy = json_decode($json);

echo $yummy->toppings[2]->id; //5004

Passing true as the second argument to json_decode()

When you do this, instead of objects you'll get associative arrays - arrays with strings for keys. Again you access the elements thereof as usual, e.g. $array['key'].

$json = '
{
    "type": "donut",
    "name": "Cake",
    "toppings": [
        { "id": "5002", "type": "Glazed" },
        { "id": "5006", "type": "Chocolate with Sprinkles" },
        { "id": "5004", "type": "Maple" }
    ]
}';

$yummy = json_decode($json, true);

echo $yummy['toppings'][2]['type']; //Maple

Accessing associative array items

When decoding a JSON object to an associative PHP array, you can iterate both keys and values using the foreach (array_expression as $key => $value) syntax, eg

$json = '
{
    "foo": "foo value",
    "bar": "bar value",
    "baz": "baz value"
}';

$assoc = json_decode($json, true);
foreach ($assoc as $key => $value) {
    echo "The value of key '$key' is '$value'", PHP_EOL;
}

Prints

The value of key 'foo' is 'foo value'
The value of key 'bar' is 'bar value'
The value of key 'baz' is 'baz value'


Don't know how the data is structured

Read the documentation for whatever it is you're getting the JSON from.

Look at the JSON - where you see curly brackets {} expect an object, where you see square brackets [] expect an array.

Hit the decoded data with a print_r():

$json = '
{
    "type": "donut",
    "name": "Cake",
    "toppings": [
        { "id": "5002", "type": "Glazed" },
        { "id": "5006", "type": "Chocolate with Sprinkles" },
        { "id": "5004", "type": "Maple" }
    ]
}';

$yummy = json_decode($json);

print_r($yummy);

and check the output:

stdClass Object
(
    [type] => donut
    [name] => Cake
    [toppings] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 5002
                    [type] => Glazed
                )

            [1] => stdClass Object
                (
                    [id] => 5006
                    [type] => Chocolate with Sprinkles
                )

            [2] => stdClass Object
                (
                    [id] => 5004
                    [type] => Maple
                )

        )

)

It'll tell you where you have objects, where you have arrays, along with the names and values of their members.

If you can only get so far into it before you get lost - go that far and hit that with print_r():

print_r($yummy->toppings[0]);
stdClass Object
(
    [id] => 5002
    [type] => Glazed
)

Take a look at it in this handy interactive JSON explorer.

Break the problem down into pieces that are easier to wrap your head around.


json_decode() returns null

This happens because either:

  1. The JSON consists entirely of just that, null.
  2. The JSON is invalid - check the result of json_last_error_msg or put it through something like JSONLint.
  3. It contains elements nested more than 512 levels deep. This default max depth can be overridden by passing an integer as the third argument to json_decode().

If you need to change the max depth you're probably solving the wrong problem. Find out why you're getting such deeply nested data (e.g. the service you're querying that's generating the JSON has a bug) and get that to not happen.


Object property name contains a special character

Sometimes you'll have an object property name that contains something like a hyphen - or at sign @ which can't be used in a literal identifier. Instead you can use a string literal within curly braces to address it.

$json = '{"@attributes":{"answer":42}}';
$thing = json_decode($json);

echo $thing->{'@attributes'}->answer; //42

If you have an integer as property see: How to access object properties with names like integers? as reference.


Someone put JSON in your JSON

It's ridiculous but it happens - there's JSON encoded as a string within your JSON. Decode, access the string as usual, decode that, and eventually get to what you need.

$json = '
{
    "type": "donut",
    "name": "Cake",
    "toppings": "[{ \"type\": \"Glazed\" }, { \"type\": \"Maple\" }]"
}';

$yummy = json_decode($json);
$toppings = json_decode($yummy->toppings);

echo $toppings[0]->type; //Glazed

Data doesn't fit in memory

If your JSON is too large for json_decode() to handle at once things start to get tricky. See:

  • Processing large JSON files in PHP
  • How to properly iterate through a big json file

How to sort it

See: Reference: all basic ways to sort arrays and data in PHP.

2 of 3
5
<?php
$jsonData = '{
    "type": "donut",
    "name": "Cake",
    "toppings": [
        { "id": "5002", "type": "Glazed" },
        { "id": "5006", "type": "Chocolate with Sprinkles" },
        { "id": "5004", "type": "Maple" }
    ]
}';

// Decode the JSON
$data = json_decode($jsonData, true);

// Access the data
$type = $data['type'];
$name = $data['name'];
$toppings = $data['toppings'];

// Access individual topping details
$firstTopping = $toppings[0];
$firstToppingId = $firstTopping['id'];
$firstToppingType = $firstTopping['type'];

// Print the data
echo "Type: $type\n";
echo "Name: $name\n";
echo "First Topping ID: $firstToppingId\n";
echo "First Topping Type: $firstToppingType\n";
?>

In this example, json_decode() is used to decode the JSON data into a PHP associative array. You can then access the individual elements of the array as you would with any PHP array.

🌐
W3Schools
w3schools.com › php › php_json.asp
PHP and JSON
The json_decode() function is used to decode a JSON object into a PHP object or an associative array.
🌐
Temboo
temboo.com › php › parsing-json
Parsing JSON in PHP
Select PHP from the drop down menu at the top of the page.. 2 Enter any search term you want for the Query input and click Generate Code to test the Choreo from our website. 3 You get a whole bunch of JSON in the Response output. These are the results of the search. Next we'll see how to parse through this response in PHP and pick out only the pieces we're interested in.
🌐
Reddit
reddit.com › r/php › json parser: parse json of any dimension from any source
r/PHP on Reddit: JSON Parser: parse JSON of any dimension from any source
June 16, 2023 -

Hello everybody, I'm quite happy to share that I finally released JSON Parser, a framework-agnostic PHP package that can parse JSON of any dimension and from any source in a memory-efficient way.

This package leverages generators to keep only one key and value in memory at a time. It can recursively do this for larger JSONs as well.

It also provides pointers to extract only the necessary sub-trees instead of reading all the JSON.

If your app works with JSON, this package can help you save a significant amount of memory as it consumes only a few KB of memory, regardless of the JSON size.

Feel free to check it out and let me know what you think! :)

https://github.com/cerbero90/json-parser

🌐
CodeSignal
codesignal.com › learn › courses › handling-json-files-with-php › lessons › introduction-to-parsing-json-with-php
Working with JSON in PHP | CodeSignal Learn
The json_decode function reads the JSON string and converts it into a PHP array using the option true to transform objects into associative arrays. ... Finally, let's print out the parsed data to verify our parsing is successful using PHP's echo and json_encode for a formatted output.
🌐
Medium
agungpp.medium.com › decoding-large-json-files-efficiently-in-php-json-decode-vs-streaming-with-json-machine-b90273e09aba
Decoding Large JSON Files Efficiently in PHP — json_decode() vs Streaming with JSON Machine | by Agung Putra Pasaribu | Medium
October 10, 2025 - Instead, they read and yield items one by one as the file is parsed. ... <?php use JsonMachine\Items; $start = microtime(true); $memoryBefore = memory_get_usage(true); $items = Items::fromFile('data/large.json'); $count = 0; foreach ($items as $item) { $count++; // You can process $item here (e.g.
🌐
GitHub
github.com › BaseMax › JPOPHP
GitHub - BaseMax/JPOPHP: JSON Parser Object PHP is a library for parsing the data in JSON format. · GitHub
Tiny Library for parse JSON. JPOPHP (PHPJsonParser) can encode and decode data in JSON format.
Author   BaseMax
🌐
JSON Formatter
jsonformatter.org › json-parser
JSON Parser Online to parse JSON
Without coding or any hassle, developers can parse JSON data. Know more about JSON : How to Create JSON File? JSON Full Form · What is JSON? JSON Example with all data types including JSON Array. Python Pretty Print JSON · Read JSON File Using Python · Validate JSON using PHP ·
🌐
Tutorial Republic
tutorialrepublic.com › php-tutorial › php-json-parsing.php
How to Encode and Decode JSON Data in PHP - Tutorial Republic
In this tutorial you will learn how to encode and decode JSON data in PHP. JSON stands for JavaScript Object Notation. JSON is a standard lightweight data-interchange format which is quick and easy to parse and generate.