SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Evangelist
    Join Date
    Jan 2005
    Posts
    425
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Multidimensional array sorting question

    I'm wanting to sort the following array structure (the below array is print_r($glossary_index)) so that it is by length of the post_title (longest to shortest) :
    Array
    (
    [728] => stdClass Object
    (
    [ID] => 728
    [post_author] => 1
    [post_date] => 2010-07-15 17:34:34
    [post_date_gmt] => 2010-07-15 07:34:34
    [post_content] => Somebody you owe money to
    [post_title] => Creditor
    [post_excerpt] =>
    [post_status] => publish
    [comment_status] => closed
    [ping_status] => closed
    [post_password] =>
    [post_name] => creditor
    [to_ping] =>
    [pinged] =>
    [post_modified] => 2010-07-16 11:58:28
    [post_modified_gmt] => 2010-07-16 01:58:28
    [post_content_filtered] =>
    [post_parent] => 0
    [guid] => ...
    [menu_order] => 0
    [post_type] => glossary
    [post_mime_type] =>
    [comment_count] => 0
    [filter] => raw
    )

    [725] => stdClass Object
    (
    [ID] => 725
    [post_author] => 1
    [post_date] => 2010-07-15 17:10:24
    [post_date_gmt] => 2010-07-15 07:10:24
    [post_content] => yada yada yda
    [post_title] => Creditor’s Petition
    [post_excerpt] =>
    [post_status] => publish
    [comment_status] => closed
    [ping_status] => closed
    [post_password] =>
    [post_name] => creditors-petition
    [to_ping] =>
    [pinged] =>
    [post_modified] => 2010-07-15 17:38:06
    [post_modified_gmt] => 2010-07-15 07:38:06
    [post_content_filtered] =>
    [post_parent] => 0
    [guid] => ...
    [menu_order] => 0
    [post_type] => glossary
    [post_mime_type] =>
    [comment_count] => 0
    [filter] => raw
    )

    )
    So in the above scenario it would become:
    Array
    (
    [725] => stdClass Object
    (
    [ID] => 725
    [post_author] => 1
    [post_date] => 2010-07-15 17:10:24
    [post_date_gmt] => 2010-07-15 07:10:24
    [post_content] => yada yada yda
    [post_title] => Creditor’s Petition
    [post_excerpt] =>
    [post_status] => publish
    [comment_status] => closed
    [ping_status] => closed
    [post_password] =>
    [post_name] => creditors-petition
    [to_ping] =>
    [pinged] =>
    [post_modified] => 2010-07-15 17:38:06
    [post_modified_gmt] => 2010-07-15 07:38:06
    [post_content_filtered] =>
    [post_parent] => 0
    [guid] => ...
    [menu_order] => 0
    [post_type] => glossary
    [post_mime_type] =>
    [comment_count] => 0
    [filter] => raw
    )

    [728] => stdClass Object
    (
    [ID] => 728
    [post_author] => 1
    [post_date] => 2010-07-15 17:34:34
    [post_date_gmt] => 2010-07-15 07:34:34
    [post_content] => Somebody you owe money to
    [post_title] => Creditor
    [post_excerpt] =>
    [post_status] => publish
    [comment_status] => closed
    [ping_status] => closed
    [post_password] =>
    [post_name] => creditor
    [to_ping] =>
    [pinged] =>
    [post_modified] => 2010-07-16 11:58:28
    [post_modified_gmt] => 2010-07-16 01:58:28
    [post_content_filtered] =>
    [post_parent] => 0
    [guid] => ...
    [menu_order] => 0
    [post_type] => glossary
    [post_mime_type] =>
    [comment_count] => 0
    [filter] => raw
    )

    )

  2. #2
    SitePoint Enthusiast
    Join Date
    Jul 2010
    Posts
    63
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi LuckyB,
    is this what you want?
    PHP Code:
    function sortByLength($a,$b){
      if(
    $a == $b) return 0;
      return (
    strlen($a) > strlen($b) ? -1);
    }

    $array = array("bbbbb""dog""cat""aaa""aaaaaaa");

    usort($array,'sortByLength');

    echo 
    "<pre>";
    print_r($array); 
    You can View the complete details here.
    http://php.net/manual/en/function.usort.php

  3. #3
    SitePoint Evangelist
    Join Date
    Jan 2005
    Posts
    425
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Possibly, but not sure if that would work with such a multidimensional array, the example array is very simple.

    I need to sort.order those arrays by [post_title]. Can't see that fairly simple code doing that.

  4. #4
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    LuckyB, the process is just the same. You simply need to refer to whatever values it is that you want to sort on: in this case the length of the post_title property.

    PHP Code:
    function sortByLength($a,$b){
        return 
    strlen($b->post_title) - strlen($a->post_title);

    Salathe
    Software Developer and PHP Manual Author.

  5. #5
    SitePoint Addict
    Join Date
    Sep 2004
    Posts
    211
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Possibly, but not sure if that would work with such a multidimensional array, the example array is very simple.
    You keep saying "multidimensional array", but $glossary_index is NOT a multidimensional array. It is an array of objects. As far as I know multidimensional arrays are arrays having as elements other arrays. Maybe that's what is messing up your thinking. Salathe's solution is what you're looking for.

  6. #6
    SitePoint Evangelist
    Join Date
    Jan 2005
    Posts
    425
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    wow, thanks much. Would never have guessed it was such a simple solution! My apologise, and thanks again!!


Bookmarks

Posting Permissions

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