0

I seem to be stuck again! - I asked this question on friday which @Bartdude was great with answering - Array output and date format

This outputted an array like this -

[0]=> object(stdClass)#270 (3) 
{ 
["Month(StartDate)"]=> string(1) "4" 
["Year(StartDate)"]=> string(4) "2013" 
["nrOfEvents"]=> string(2) "12" 
} 
[1]=> object(stdClass)#176 (3) 
{ 
["Month(StartDate)"]=> string(1) "5" 
["Year(StartDate)"]=> string(4) "2013" 
["nrOfEvents"]=> string(2) "19" 
} 
[2]=> object(stdClass)#114 (3) 
{ 
["Month(StartDate)"]=> string(1) "6" 
["Year(StartDate)"]=> string(4) "2013" 
["nrOfEvents"]=> string(2) "12" 
} 
}

Then after some searching through Stack Exchange I found this would return nrofEvents -

echo $results[0]->nrOfEvents
// Outputs - 12

I am struggling to understand how to access ["Month(StartDate)"] in the array in a foreach loop.

My end goal is to achieve a table of months that has a month name, for example

| Mar (12) | Apr(19) | Jun(12) |

| Jul(3) | Aug(4) | Sep(5) |

Any help in helping me understand would be fantastic and thanks in advance.

4

4 回答 4

0

尝试这个:

foreach($array as $object) {
   echo $object->{'Month(StartDate)'}, '<br>';
}
于 2013-05-28T09:55:41.833 回答
0
SELECT Month(startdate) AS month, count(id) as nrOfEvents
FROM wp_myEventDates
GROUP BY Month(startdate)

您将能够通过以下方式获取月份值:

echo $results[0]->month
// Outputs - 4
于 2013-05-28T09:57:04.730 回答
0

用这个:

$array = [array of dates]
$formats = array();

foreach($array as $key => $object)
{
    $formats[] = date('M', $array[$key]->{Month(StartDate)}) . ' (' . $array[$key]->nrOfEvents . ')';
}

$formats = implode(' | ', $formats);

但是:您应该认真考虑重命名数组值。(并且)对变量名无效。

于 2013-05-28T09:57:27.327 回答
0

这允许访问Month(StartDate)

$results[0]->{'Month(StartDate)'}

但你最好使用数组

于 2013-05-28T09:54:46.923 回答