0

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,

Joran Den Houting
  • 3,149
  • 3
  • 21
  • 51
Scott Bowers
  • 175
  • 3
  • 13
  • Already tried this? http://php.net/manual/de/function.array-multisort.php – TiMESPLiNTER Oct 31 '13 at 10:06
  • 1
    have a look there, possible duplicate : http://stackoverflow.com/questions/17364127/reference-all-basic-ways-to-sort-arrays-and-data-in-php – Nicolas Brugneaux 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 Answers2

1

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))

OlivierH
  • 3,875
  • 1
  • 19
  • 32
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.

José Antonio Postigo
  • 2,674
  • 24
  • 17