Try this:
foreach($array as $object) {
echo $object->{'Month(StartDate)'}, '<br>';
}
Frage
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.
Lösung
Try this:
foreach($array as $object) {
echo $object->{'Month(StartDate)'}, '<br>';
}
Andere Tipps
This allows to access Month(StartDate)
:
$results[0]->{'Month(StartDate)'}
But you would better be using arrays
SELECT Month(startdate) AS month, count(id) as nrOfEvents
FROM wp_myEventDates
GROUP BY Month(startdate)
You will be able to the month value by:
echo $results[0]->month
// Outputs - 4
Use this:
$array = [array of dates]
$formats = array();
foreach($array as $key => $object)
{
$formats[] = date('M', $array[$key]->{Month(StartDate)}) . ' (' . $array[$key]->nrOfEvents . ')';
}
$formats = implode(' | ', $formats);
HOWEVER: You should seriously consider renaming your array values. (
and )
are not valid for variable names.