2

Sorry if this is simple, I am a PHP newbie.

Using a REST based API I am getting the following results back from the system I am accessing:

$results = Array ( 
    [0] => stdClass Object ( 
        [ID] => 4d74fcda000291fe949dce44b892c57a   
        [name] => File Quarterly Taxes 
        [objCode] => TASK 
        [status] => NEW 
        [plannedCompletionDate] => 2011-09-09T07:30:00:000-0600 
        [description] => 

        [project] => stdClass Object ( 
            [ID] => 4d3d9cb00000829953755920c930f68a
            [name] => 2010 Accounting 
            [objCode] => PROJ
        )
    )
    [1] => stdClass Object ( 
        [ID] => 4d74fcda000291fd43a0b7c9c8224d3a 
        [name] => File Quarterly Taxes 
        [objCode] => TASK 
        [status] => NEW 
        [plannedCompletionDate] => 2011-06-10T07:30:00:000-0600 
        [description] => 

        [project] => stdClass Object (
            [ID] => 4d3d9cb00000829953755920c930f68a 
            [name] => 2010 Accounting 
            [objCode] => PROJ
        )
    ) 
    [2] => stdClass Object ( 
        [ID] => 4d74fcda000291ffd91d63e25945d2be 
        [name] => File Quarterly Taxes 
        [objCode] => TASK 
        [status] => NEW 
        [plannedCompletionDate] => 2012-01-13T07:30:00:000-0700 
        [description] => 

        [project] => stdClass Object ( 
            [ID] => 4d3d9cb00000829953755920c930f68a 
            [name] => 2010 Accounting 
            [objCode] => PROJ
        )
    )
) 

How do I take that array and sort it by the plannedCompletionDate in the object?

2
  • 1
    echo a <pre> tag before you output that mess so it will be indented correctly. Or copy it from "view source" Commented May 10, 2011 at 18:10
  • or use header('Content-type:text/plain;charset=utf-8'); Commented May 10, 2011 at 18:14

2 Answers 2

2
function sort_callback( $a, $b ) {
    if( $a->plannedCompletionDate == $b->plannedCompletionDate ) {
        return 0;
    }
    return ( $a->plannedCompletionDate > $b->plannedCompletionDate ) ? 1 : -1;
}

usort( $results, 'sort_callback' );
Sign up to request clarification or add additional context in comments.

Comments

2

write a custom function which compares 2 objects and use it as callback function for usort http://php.net/manual/en/function.usort.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.