0

Possible Duplicate:
How do I sort a multidimensional array in php
Sorting an associative array in PHP

I have an array of hours, titles and descriptions, but the hours are disordered (12:00, 04:15, 18:30, 10:20...)

This is my PHP code:

$content = simplexml_load_file( $sampleXML );
foreach( $content->item as $item ) {

    echo $item->hour;
    echo $item->title;
    echo $item->description;    
}

and this is a sample of the XML:

<item>
    <hour>12:00</hour>
    <title>Sample Title 1</title>
    <description>Sample Description 1</description>
</item>

<item>
    <hour>04:15</hour>
    <title>Sample Title 1</title>
    <description>Sample Description 1</description>
</item>

<item>
    <hour>18:30</hour>
    <title>Sample Title 1</title>
    <description>Sample Description 1</description>
</item>

I need to sort the hours with it's corresponding title and description. How can I do this?

Community
  • 1
  • 1
isiaatz
  • 1,085
  • 3
  • 14
  • 21

2 Answers2

1

This should work for 24 hour format like in your example:

usort($a, create_function('$a, $b', 'return strcmp($a->hour, $b->hour);'));
roychri
  • 2,636
  • 1
  • 17
  • 27
0

You can use usort() and pass it a function that would know how to compare two of the XML nodes using the time to order them.

Crashspeeder
  • 4,101
  • 2
  • 14
  • 21