简体   繁体   English

object(stdClass)数组和返回值

[英]object(stdClass) Array and returning values

I seem to be stuck again! 我似乎又被卡住了! - I asked this question on friday which @Bartdude was great with answering - Array output and date format -我在星期五问了这个问题,@ Bartdude最适合回答- 数组输出和日期格式

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 - 然后,在通过Stack Exchange进行一些搜索之后,我发现它将返回nrofEvents-

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

I am struggling to understand how to access ["Month(StartDate)"] in the array in a foreach loop. 我正在努力了解如何在foreach循环中访问数组中的[“ Month(StartDate)”]。

My end goal is to achieve a table of months that has a month name, for example 我的最终目标是获得一个具有月份名称的月份表,例如

| | Mar (12) | 3月 (12)| Apr (19) | 4月 (19)| Jun (12) | 6月 (12)|

| | Jul (3) | 7月 (3)| Aug (4) | 8月 (4)| Sep (5) | 9月 (5)|

Any help in helping me understand would be fantastic and thanks in advance. 任何帮助我理解的帮助都非常棒,在此先感谢。

This allows to access Month(StartDate) : 这允许访问Month(StartDate)

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

But you would better be using arrays 但是你最好使用数组

Try this: 尝试这个:

foreach($array as $object) {
   echo $object->{'Month(StartDate)'}, '<br>';
}
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. ()对变量名无效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM