So far so good, but what about associative arrays? With these types of arrays, you often want to access each element’s key as well as its value. The above script produces the following output: $directors = array( "Alfred Hitchcock", "Stanley Kubrick", "Martin Scorsese", "Fritz Lang" ) This code runs after the loop finishesįor example, the following script loops through a list of movie directors in an indexed array, displaying each director’s name as it goes: The simplest way to use foreach is when looping through the values in an indexed array. In this article you learn the basic syntax of foreach, and see how to use it to loop through both indexed and associative arrays. However, there’s a much easier way to loop through arrays: the foreach construct. In Counting PHP Array Elements Using count(), I showed how you can use a for loop along with the count() function to loop through an array. For example, you may want to display each value in an HTML table, or give each element a new value. Our first example is to find all people who owns a 'Tesla' from our sample JSON.Often you need to move through all the elements in a PHP array so that you can do something with each element’s value. Now we learn how to search a nested JSON for a specific value. So far, we have seen how to access all objects at a particular level. Here is another example to print the number of cars each person has.Įcho( $elem.": ".count($elem)) To print all the names and corresponding cars. If you have an array inside a nested JSON object then use two foreach loops to access the inner array element. To print all the names and corresponding favourite colour.Įcho( $elem." - ".$elem ) You can access this inner JSON object just as you access a multidimensional associative array. For each person, there is a key named favourite whose value is another JSON object. In our example JSON, there are three JSON objects each corresponding to the details about a person. The value of a JSON can be another JSON object. For example, to print all the name and id numbers from our example JSON document:Īccessing Nested objects inside a JSON array 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. A JSON document can have JSON objects nested inside other JSON objects.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |