Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to arrange my array (below) by [date], but to no avail as of yet :(

Needed to pad this out with some more text as apparently it's mostly code and stackoverflow doesn't like this :/

Array
(
    [0] => gapiReportEntry Object
        (
            [metrics:gapiReportEntry:private] => Array
                (
                    [uniquepageviews] => 0
                    [pageviews] => 0
                    [visits] => 0
                    [visitors] => 0
                )

            [dimensions:gapiReportEntry:private] => Array
                (
                    [date] => 20131009
                )

        )

    [1] => gapiReportEntry Object
        (
            [metrics:gapiReportEntry:private] => Array
                (
                    [uniquepageviews] => 1
                    [pageviews] => 1
                    [visits] => 1
                    [visitors] => 1
                )

            [dimensions:gapiReportEntry:private] => Array
                (
                    [date] => 20131026
                )

        )
)

Can anyone help me? Thanks,

share|improve this question
    
Already tried this? php.net/manual/de/function.array-multisort.php –  TiMESPLiNTER Oct 31 '13 at 10:06
1  
have a look there, possible duplicate : stackoverflow.com/questions/17364127/… –  mortaga Oct 31 '13 at 10:06
    
Tried multisort already, doesn't seem to work unless i'm doing it wrong. foreach($ga->getResults() as $i => $result) { $dates[$i] = $result->getDate(); } array_multisort($dates, SORT_ASC, $ga->getResults()); –  Scott Bowers Oct 31 '13 at 10:09

2 Answers 2

up vote 1 down vote accepted

You can use standard php usort function to do it :

$array = usort($array, function ($a, $b) use ($array){
    return strcmp($a -> dimensions -> date,  $b -> dimensions -> date);
}):

Notice I use a closure here (see http://php.net/manual/fr/function.usort.php))

share|improve this answer

Considering the array is called $array, try this code:

$dates = array();
foreach($array as $v)
    $dates[] = $v['dimensions:gapiReportEntry:private'];

asort($dates);

$sorted_array = array();
foreach($dates as $k => $v)
    $sorted_array[$k] = $array[$k];

You will have the result in the $sorted_array variable.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.