From the course: Learning the Standard PHP Library
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
Filtering values from JSON - PHP Tutorial
From the course: Learning the Standard PHP Library
Filtering values from JSON
- Web services frequently provide data feeds using JavaScript object notation or JSON for short. It consumes less bandwidth than the equivalent XML and is relatively easy to pass with JavaScript. Thanks to SPL, it's also very easy to handle with PHP. This is courses.json, which is the common data directory. It contains exactly the same data as courses.xml except that it's in JSON format. So let's create a new PHP page to pass this data. And we're going to save it as json_decode.php. And we can load the data from the JSON file using file.getcontents. We'll save it as courses and we need to insert the path, which is common/data/courses.json. To convert the JSON into a format that PHP can handle, all that's necessary is to use the JSON decode function. We'll reassign it to courses. So json_decode, we pass it the raw JSON, which is stored as courses, and we can then inspect that using print_r. So if we just save that page and load it into a browser, it looks pretty difficult to read…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.