0

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

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
                )

        )
)
2
  • Already tried this? php.net/manual/de/function.array-multisort.php Commented Oct 31, 2013 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()); Commented Oct 31, 2013 at 10:09

2 Answers 2

1

You can use standard php usort function to do it :

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

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

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.