Results 1 to 6 of 6
  1. #1
    Join Date
    Dec 2006
    Posts
    76

    Question on array sorting in php

    I have the following array that I printed using print_r

    PHP Code:
    [item] => Array
            (
                [
    0] => SimpleXMLElement Object
                    
    (
                        [
    title] => Title 1
                        
    [link] => Link 1
                        
    [description] => Description 1
                        
    [guid] => guid 1
                        
    [pubDate] => 9
                    
    )

                [
    1] => SimpleXMLElement Object
                    
    (
                        [
    title] => Title 2
                        
    [link] => Link 2
                        
    [description] => Description 2
                        
    [guid] => guid 2
                        
    [pubDate] => 10
                    
    )
            ) 
    I need to be able to sort it by [pubDate] in highest to lowest order.

    How would I do this?

    Thanx

  2. #2
    Join Date
    Dec 2007
    Location
    Lebanon
    Posts
    413
    you can use your pubDate as Key and then ksort($item);

  3. #3
    Join Date
    Dec 2006
    Posts
    76
    It's a multi-dimensional array which has to stay in the same format.

  4. #4
    Join Date
    Feb 2005
    Location
    Australia
    Posts
    5,842
    You still need to do as Hicham suggested: create an additional array keyed on pubDate containing the same objects. Ideally you do this at the same time as creating your original array; if not you'll have to iterate through the original, eg. using array_map.
    Chris

    "Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them." - Laurence J. Peter

  5. #5
    Join Date
    Nov 2005
    Posts
    123
    Use array_multisort():
    PHP Code:
    foreach ($my_array as $key => $val) {
        
    $tmp[$key] = $val['pubDate'];
    }
    array_multisort($tmpSORT_DESC$my_array); 
    That should work on the array as shown. (Although I think there's more to it than what you posted. If so adjust the keys for the values of $val assigned to $tmp as needed.)

  6. #6
    Join Date
    Nov 2005
    Posts
    123
    Assume an XML document like this:
    HTML Code:
    <rss>
    <channel>
        <title>Test Doc</title>
    </channel>
    <item>
        <title>Title 1</title>
        <link>Link 1</link>
        <description>Description 1</description>
        <guid>guid 1</guid>
        <pubDate>9</pubDate>
    </item>
    <item>
        <title>Title 2</title>
        <link>Link 2</link>
        <description>Description 2</description>
        <guid>guid 2</guid>
        <pubDate>10</pubDate>
    </item>
    </rss>
    Then this code:
    PHP Code:
    $xml_obj simplexml_load_file('doc.xml');
    $my_array $xml_obj->xpath('item');
    foreach (
    $my_array as $key => $val) {
        
    $val = (array) $val;  // Notice explicit cast.
        
    $tmp[$key] = $val['pubDate'];
    }
    array_multisort($tmpSORT_DESC$my_array); 
    will give you this:
    Code:
    Array
    (
        [0] => SimpleXMLElement Object
            (
                [title] => Title 2
                [link] => Link 2
                [description] => Description 2
                [guid] => guid 2
                [pubDate] => 10
            )
        [1] => SimpleXMLElement Object
            (
                [title] => Title 1
                [link] => Link 1
                [description] => Description 1
                [guid] => guid 1
                [pubDate] => 9
            )
    )
    Last edited by bigfan; 01-18-2008 at 11:53 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •