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 - PHP Tutorial
From the course: Learning the Standard PHP Library
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…
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.
Contents
-
-
-
-
-
-
-
-
-
(Locked)
Doubly linked lists, stacks, and queues3m 10s
-
(Locked)
Sorting XML and JSON with SplDoublyLinkedList8m 26s
-
(Locked)
Using SplStack and SplQueue6m 25s
-
(Locked)
Understanding heaps2m 55s
-
(Locked)
SplMinHeap and SplMaxHeap5m 3s
-
(Locked)
Sorting XML and JSON with SplHeap7m 9s
-
(Locked)
Finding important information with SplPriorityQueue5m 32s
-
(Locked)
Keeping priority items in chronological order4m 23s
-
(Locked)
Speeding up array access with SplFixedArray6m 48s
-
(Locked)
-