How to Parse JSON with PHP



PHP







In this article, we show how to parse JSON data with PHP.

By parsing through JSON data, this means that we are able to sort through it and look for what we want from the JSON data.

So, for example, if we have a list of people objects that contains the city and state of the person, we can sort through this JSON data and only return people who live in the state of Florida, for instance.

So below we show the code how to access JSON data using PHP.



So, we first have json data stored in the variable $jsonData.

PHP cannot read or handle json data, as is. So it converts the json data into something that PHP can understand through the json_decode function. The json_decode() function is a built-in PHP function that converts JSON data into a multidimensional PHP array. We then can handle all of the data after the json_decode function like a multidimensional array (which it now is).

So in the above, we output all of the data of by accessing each individual data item in the multidimensional array.

Running the above PHP code, we get the following output shown below.

Actual PHP Output


Bill Smith
Fort Lauderdale
Florida

Vanessa Halls
New York City
New York

Ryan Mitchells
Miami
Florida




So we can access all of the data individually that we want through the above code, but this is not really parsing through the data.

Let's say we are actually looking for something specific.

We want to find all people who live in the state of Florida.

To do this we use the following PHP code below.



So we have the same JSON data as before of people their name, city, and state.

We then decode this JSON data through the json_decode() function, which creates a multidimensional PHP array.

We have the number of elements in the array, so that we know how many people it's composed of (we need this later for the for loop). We place this value in the variable $count.

We then creat a foor loop. Since arrays begin at element 0, this for loop starts at the value of 0. It counts up 1 value after each iteration. When it gets one less than the count of the number of elements in the array, this means it has looped through all elements in the array.

We then want to know which of these people live in Florida. So we access the state of each person through the code, $people[$i]["state"]. If this value is equal to Florida, then we know that that person lives in Florida. If this is the case, we echo out all values of that person, including the name, city, and state of that person.

And this is how we can parse JSON data with PHP.

So, as a recap, first we must take the json data and decode it through the json_decode() function. This converts the json data to a PHP multidimensional array. Then we can just handle the data as you would a multidimensional array. You can access a particular value in an array and use the standard programming methods to extract data, such as through using for loops.

If I take the above PHP code and run it, I get the following output shown below.

Actual PHP Output


Bill Smith
Fort Lauderdale
Florida

Ryan Mitchells
Miami
Florida



So now you see how you can access any element and use for loops.

If we want to see whether a person has the name "Vanessa Halls" from the JSON data and output her city and state, we can use the following PHP code.



So this finds if there is a person named Vanessa Halls and finds her city and state.

Running the full PHP code gives us the following output.

Actual PHP Output


New York City
New York



So, by now, you can pretty much see the trend of how you can manipulate what was originally JSON data and convert it to PHP through the json_decode() function. This data gets converted from JSON to a PHP multidimensional array. We then can parse through this data as you would any PHP multidimensional array.

So this is an easy way to parse JSON with PHP.


Related Resources

How to Write JSON with PHP

How to Write JSON with PHP

HTML Comment Box is loading comments...