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
    )   

)

wow, thanks much. Would never have guessed it was such a simple solution! My apologise, and thanks again!!

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.


function sortByLength($a,$b){
    return strlen($b->post_title) - strlen($a->post_title);
}

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.

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.

Hi LuckyB,
is this what you want?


function sortByLength($a,$b){
  if($a == $b) return 0;
  return (strlen($a) > strlen($b) ? -1 : 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