From the course: Learning the Standard PHP Library

Unlock the full course today

Join today to access over 22,700 courses taught by industry experts or purchase this course individually.

Sorting XML and JSON with SplHeap

Sorting XML and JSON with SplHeap - PHP Tutorial

From the course: Learning the Standard PHP Library

Start my 1-month free trial

Sorting XML and JSON with SplHeap

- The SplHeap class is designed for situations where you need greater control over how items are sorted or if they can't be directly compared. I've opened splheap.php, which you can find in the Chapter 7 folder of the exercise files. It loads the data in courses.json, and then uses json_decode to pass it. The json_decode function produces an array of objects with multiple properties. To sort the objects, we need to get one of the properties and use that for comparison. We'll use the title property. After working with the JSON data, we'll take a quick look at using SplHeap with the SimpleXML code that's commented out on Line 2. There's subtle but important difference in how the XML data needs to be handled. SplHeap is an abstract class, so you need to extend it and definite your own compare method. For the purposes of this exercise, we'll define the class in the same file. So we'll add a few extra lines at the top, and we'll call the class SortCourses and it needs to extend SplHeap…

Contents