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.

Sorting XML and JSON with SplDoublyLinkedList

Sorting XML and JSON with SplDoublyLinkedList - PHP Tutorial

From the course: Learning the Standard PHP Library

Start my 1-month free trial

Sorting XML and JSON with SplDoublyLinkedList

A doubly-linked list can store not only simple values such as strings and numbers, it can also be used with complex values such as objects. This is doublylinked.php, which you can find in the Chapter 7 folder of the exercise files. It loads the data in courses.xml, a Simple XML, and then loops through it to display the author, title and duration of each course. If we load this page into a browser, each course is displayed in the same order as in the original .xml. Let's use a doubly-linked list to sort the courses according to the author's last name, using an algorithm called "insertion sort". This relies on SplDoublyLinkedList's add() method, so it requires PHP 5.5 or later. So let's get to the code. I want to use $courses as the name for the doubly-linked list. So we need to rename the variable that loads the data on Line 2. So we'll call that simply, $data. And on the next line, we'll create our doubly-linked list, which we'll call $courses. That's new SplDoublyLinkedList(); The…

Contents