Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a multidimensional array of elements that are grouped by months, for example:

Array
(
    [2013-01] => Array
        (
            [0] => Array
                (
                    [Project] => Array
                        (
                            [id] => 1
                            [user_id] => 1
                            [created] => 2013-04-08 01:00:56
                            [modified] => 2013-04-08 01:01:40
                            [vId] => 7
                        )
                )
        )

    [2013-04] => Array
        (
            [2] => Array
                (
                    [Project] => Array
                        (
                            [id] => 1
                            [user_id] => 1
                            [created] => 2013-04-08 01:00:56
                            [modified] => 2013-04-08 01:01:40
                            [refimg] => uploads/smallRef.png
                        )
                )
            [3] => Array
                (
                    [Project] => Array
                        (
                            [id] => 1
                            [user_id] => 1
                            [created] => 2013-04-08 01:00:56
                            [modified] => 2013-04-08 01:01:40
                        )
                )
            [4] => Array
                (
                    [Project] => Array
                        (
                            [id] => 1
                            [user_id] => 1
                            [created] => 2013-04-08 01:00:56
                            [modified] => 2013-04-08 01:01:40
                        )
                )
        )
)

Now I want to loop through months, and for each month I want to perform a count on the array for the specific month:

$currMonth = date('Y-m-d');
while (strtotime($currMonth) >= strtotime($firstMonth)) {
    $curM = date('Y-m', strtotime($currMonth));
    count($grouparr[$curM]);
    $currMonth = date ("Y-m-d", strtotime("-1 month", strtotime($currMonth)));
}

This does not seem to work. I get the following error:

Uncaught SyntaxError: Unexpected token <

If I enter the date manually everything works fine, for example if I replace this in the code above:

count($grouparr["2013-01");

I hope someone can tell me what I am doing wrong

share|improve this question
1  
There is no syntax error in this code? – bwoebi Apr 11 at 16:52
1  
There is not even the < character in the code – Ed Heal Apr 11 at 16:53
1  
you got a line number for that error? I don't even see a < in the code you posted... – mattedgod Apr 11 at 16:53
"I hope someone can tell me what I am doing wrong" - If you do not know I expect that I will not know what you are doing! – Ed Heal Apr 11 at 16:57
pls consider to accept one of the answers – michi Apr 14 at 12:34

closed as too localized by Andrew Barber Apr 12 at 19:13

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, see the FAQ.

3 Answers

up vote 0 down vote accepted
foreach($yourarray as $month) {
 echo sizeof($month);
}
share|improve this answer

IMHO, don't use the strtotime() function if you can help it. I don't know exactly what your end result may be, but let me offer a couple alternatives when iterating through an array like this.

First, if you just want to loop through the array...

foreach ($grouparr as $key => $val) {
    list($year, $month) = explode("-", $key, 2);
    printf("%s, %s has %s records",
        $month,
        $year,
        is_array($val) ? count($val) : 0
    );
}

Or maybe you want to go through the past 12 months...

for ($i = 1; $i <= 12; $i++) {
    //Use this timestamp for all your date calculations
    $mytime = mktime(0, 0, 0, date("n") - $i, 1, date("Y"));

    if (array_key_exists( date("Y-m", $mytime), $grouparr)) {
        printf("%s, %s has %s records",
            date("F", $mytime),
            date("Y", $mytime),
            is_array($grouparr[date("Y-m", $mytime)]) ? count(date("Y-m", $mytime)) : 0
        );
    }
}

These code samples can be a lot simpler, but...

share|improve this answer
foreach ($grouparr as $month => $values) {

    echo "$month: " . count($values) . "<br />";

}
share|improve this answer
why the down vote?? – michi Apr 11 at 17:06

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