note 103047 modified in function.array-multisort by danbrown

From: Date: Tue, 22 Mar 2011 14:45:28 +0000
Subject: note 103047 modified in function.array-multisort by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-178099@lists.php.net to get a copy of this message
I had a problem with sorting SimpleXMLElement list. I found a nice short solution. I have an array of ticket objects: $ticketList = $xml->Tickets; <?php // Sort by ValidOnDate $tickets = array(); $valid = array(); foreach ($ticketList as $row) { $tickets[] = $row; $valid[] = DateHandler::parseDate($row->ValidOnDate); } array_multisort($valid, SORT_DESC, $tickets); ?> Just make an array of object items you want to sort by and a new array for the same objects which will be reordered like the first array. Ps: my own parseDate(date) returns unix timestamp for different date formats. --was-- I had a problem with sorting SimpleXMLElement list. I found a nice short solution. I have an array of ticket objects: $ticketList = $xml->Tickets; // Sort by ValidOnDate $tickets = array(); $valid = array(); foreach ($ticketList as $row) { $tickets[] = $row; $valid[] = DateHandler::parseDate($row->ValidOnDate); } array_multisort($valid, SORT_DESC, $tickets); Just make an array of object items you want to sort by and a new array for the same objects which will be reordered like the first array. Ps: my own parseDate(date) returns unix timestamp for different date formats. http://php.net/manual/en/function.array-multisort.php

« previous php.notes (#178099) next »