Over a million developers have joined DZone.

Sorting Azure Table Entities by Timestamp with PHP

DZone's Guide to

Sorting Azure Table Entities by Timestamp with PHP

· Cloud Zone
Free Resource

Learn how our document data model can map directly to how you program your app, and native database features like secondary indexes, geospatial and text search give you full access to your data. Brought to you in partnership with MongoDB.

This is a short post that describes one way to sort Windows Azure Table entities by timestamp when using the Windows Azure SDK for PHP. The problem boils down to sorting an array of objects by a timestamp property, so the information here is nothing that hasn’t been done before. However, after spending some time looking for a way to use a filter in the Windows Azure SDK for PHP API, I didn’t find one, so I’m hoping this post might save others some time. In the end, my solution simply uses the PHP array_multisort function.

To illustrate the problem and my solution, I’ll reuse some of the code I wrote in this post: Accessing Windows Azure Table Storage from PHP. The important piece in that post to keep in mind is that when you define a class that inherits from Microsoft_WindowsAzure_Storage_TableEntity, the Windows Azure SDK for PHP maps specially annotated properties to “columns” in a table that has the same name as the class name. So, the example class below maps to the Contacts table and the $Name, $Address, and $Phone properties map to “columns” in that table.

class Contact extends Microsoft_WindowsAzure_Storage_TableEntity
  * @azure Name
  public $Name;

  * @azure Address
  public $Address;

  * @azure Phone
  public $Phone;

Also note that the Contact class inherits the $_partitionKey, $_rowKey, and $_timestamp properties, as well as the corresponding methods getPartitionKey(), getRowKey(), and getTimestamp().

With that class in place, I can execute a query like this to retrieve all contacts:

$tableStorageClient = new Microsoft_WindowsAzure_Storage_Table();

$contacts = $tableStorageClient->retrieveEntities("Contact", null, "Contact");
  • The first parameter in the call to retrieveEntities specifies the table from which entities are retrieved.
  • The second parameter is a filter for entities. Specifying null will retrieve all entities.
  • The last parameter specifies the object type entities are retrieved as.

To sort the entities by timestamp, I’ll use two functions (which I essentially copied from the comments in the array_multisort documentation):

function objSort(&$objArray,$indexFunction,$sort_flags=0)
     $indices = array();
     foreach($objArray as $obj)
         $indeces[] = $indexFunction($obj);
     return array_multisort($indeces,$objArray,$sort_flags);

function getIndex($obj)
     return $obj->getTimestamp();

Note: If you are using the Windows Azure SDK v3.0.0 or later, timestamps are returned as PHP DateTime objects. So, to for this sorting solution to work, you’ll need to alter the getIndex function slightly (to get the Timestamp property on a DateTime object):

function getIndex($obj)
      return $obj->getTimestamp()->getTimestamp();

Now I can sort the $contacts array like this:


That’s it! I love the comments in the PHP documentation. :-)

Discover when your data grows or your application performance demands increase, MongoDB Atlas allows you to scale out your deployment with an automated sharding process that ensures zero application downtime. Brought to you in partnership with MongoDB.


Published at DZone with permission of Brian Swan, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}