0

As an output from MySQL I got an array like this:

Array
(
    [0] => Array
        (
            [name] => Date
            [data] => Array
                (
                    [0] => 2009-07-01
                    [1] => 2009-08-01
                    [2] => 2009-09-01
                    [3] => 2010-10-01
                    [4] => 2010-11-01
                    [5] => 2010-12-01
                    [6] => 2011-01-01
                    [7] => 2011-02-01
                    [8] => 2012-03-01
                    [9] => 2012-04-01
                    [10] => 2012-05-01
                )
        )

    [1] => Array
        (
            [name] => Counts
            [data] => Array
                (
                    [0] => 13
                    [1] => 13
                    [2] => 15
                    [3] => 16
                    [4] => 17
                    [5] => 18
                    [6] => 19
                    [7] => 11
                    [8] => 14
                    [9] => 14
                    [10] => 15
                )
        )
)

For further developing I need to find the highest dates in each year. How to find last date of each year and get it's value from 'counts' array. For above code I would like to achieve something like this:

Array
(
    [0] => Array
        (
            [name] => Date
            [data] => Array
                (
                    [1] => 2009-09-01
                    [2] => 2010-12-01
                    [3] => 2011-02-01
                    [4] => 2012-05-01
                )
        )

    [1] => Array
        (
            [name] => Counts
            [data] => Array
                (
                    [1] => 15
                    [2] => 18
                    [3] => 11
                    [4] => 15
                )
           )
T_5
  • 31
  • 7
  • 5
    Show us what you've tried. This isn't difficult so surely you've attempted something. – John Conde Jan 23 '14 at 15:31
  • Yup, an easy google search wouldn't hurt, I think this might help you http://stackoverflow.com/questions/11012891/how-to-get-most-recent-date-from-an-array-of-dates – vincent kleine Jan 23 '14 at 15:38

2 Answers2

0

php has the max function . Read about it here it is very easy to use for your needs;

KB9
  • 599
  • 2
  • 7
  • 16
0
$arr = array('... your array ...');
$date = array();
$count = array();
foreach ($arr[0]['data'] as $key => $value) {
    $year = substr($value, 0, 4);
    if (!$date[$year] || $date[$year] < $value) {
        $date[$year] = $value;
        $count[$year] = $arr[1]['data'][$key];
    }
}
$date =  array_values($date); //reset the keys
$count =  array_values($count); //reset the keys
$res = array(
    array(
        'name'=>'Date',
        'data'=>$date,
    ),
    array(
        'name'=>'Count',
        'data'=>$count,
    )
);
print_r($res);
Alireza Fallah
  • 4,609
  • 3
  • 31
  • 57