![]() To access the objects inside a JSON array, the simplest and easiest method is to use a foreach loop to iterate through the array elements and fetch the data that you need. Note that the second argument in the json_decode function is set to true so that the objects returned will be associative arrays.įor more details on parsing JSON in PHP, read How to parse JSON in PHP Accessing objects inside a JSON array $json_string = file_get_contents($filepath) Firstly read the contents of the text file into a string variable using the file_get_contents() function and then use json_decode() function to convert the JSON string to a PHP variable. "favourite": Ĭonsider that our example JSON is stored in a file named "persons.txt". Let's look at some examples for accessing data a nested JSON document like below. One method is to use recursion just like you access data from a nested array or tree data structure. JSON objects are key-value pairs and there are different methods you can use to access JSON objects from a nested JSON document. So this is an easy way to parse JSON with PHP.A JSON document can have JSON objects nested inside other JSON objects. Through this data as you would any PHP multidimensional array. This data gets converted from JSON to a PHP multidimensional array. To PHP through the json_decode() function. So, by now, you can pretty much see the trend of how you can manipulate what was originally JSON data and convert it Running the full PHP code gives us the following output. So this finds if there is a person named Vanessa Halls and finds her city and state. Find if there is a Vanessa Halls and output her city and state If we want to see whether a person has the name "Vanessa Halls" from the JSON data and output her city and state, ![]() So now you see how you can access any element and use for loops. If I take the above PHP code and run it, I get the following output shown below. You can accessĪ particular value in an array and use the standard programming methods to extract data, such as through using for loops. Then we can just handle the data as you would a multidimensional array. The json data to a PHP multidimensional array. So, as a recap, first we must take the json data and decode it through the json_decode() function. ![]() If this is the case, we echo out all values of that person, including the name, city, and state of thatĪnd this is how we can parse JSON data with PHP. If this value is equal to Florida, then we know that So weĪccess the state of each person through the code, $people. ![]() We then want to know which of these people live in Florida. When it gets one less than the count of the number of elements in the array, this means it has looped It counts upġ value after each iteration. Since arrays begin at element 0, this for loop starts at the value of 0. We place this value in the variable $count. We have the number of elements in the array, so that we know how many people it's composed of (we need this laterįor the for loop). We then decode this JSON data through the json_decode() function, which creates a multidimensional PHP array. So we have the same JSON data as before of people their name, city, and state. To do this we use the following PHP code below. We want to find all people who live in the state of Florida. Let's say we are actually looking for something specific. 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. Running the above PHP code, we get the following output shown below. So in the above, we output all of the data of by accessing each individual data item in the multidimensional array. We then can handle all of the data after the json_decodeįunction like a multidimensional array (which it now is). Json_decode() function is a built-in PHP function that converts JSON data into a multidimensional PHP array. So it converts the json data into something that PHP can understand through the json_decode function. PHP cannot read or handle json data, as is. So, we first have json data stored in the variable $jsonData.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |