为了得到像
这样的数组输出array(2) { ["2012-08-16"]=> array(1) { [0]=> array(2) { ["title"]=> string(24) "RLI Class #42 Graduation" ["event_date"]=> string(10) "2012-08-16" } } ["2012-08-24"]=> array(1) { [0]=> array(2) { ["title"]=> string(10) "Test Event" ["event_date"]=> string(10) "2012-08-24" } } }
我正在尝试以下
$array = array('title' => $mytitle, 'event_date' => $myyear."-".$mymonth."-".$myday);
$events[$array['event_date']] = $array['title'];
然而,我得到的输出是
array(1) { [""]=> NULL } array(1) { ["2012-8-24"]=> string(10) "Test Event" } array(1) { ["2012-8-16"]=> string(25) "RLI Class #42 Graduation" }
如何更改代码以让$ events以所需的数组格式输出?
修改 的 为了防止代码的一些背景有助于找到解决方案,这里是
while ( $query->have_posts() ) : $query->the_post();
$mytime = get_post_meta($post->ID, 'wpcf-start-date', true); // this has 2 entries: 1>1347408000 and 2>1347408000
$mydate = date("Y-m-D",$mytime);
$mydatearr = getdate($mytime);
$myday = $mydatearr['mday']; // returns values 16 and 24
$mymonth = $mydatearr['mon']; // returns values 8 and 8
$myyear = $mydatearr['year']; // returns values 2012 and 2012
$mytitle = $post->post_title;
if (($myyear == $year) && ($mymonth == $month)) //The $year is declared earlier as 2012 and $month as 8
接下来是我用
替换的代码 $events = array(); ## Declare the new array.
foreach ($array as $line) {
$event_date = $line['event_date']; ## Set event_date to a variable
$events[$event_date] = array( 'title' => $line['title'], 'event_date' => $event_date ); ###add new value to new array with key value }
var_dump($events);
结束
endwhile;
修改 好。我好像错误地删除了我的$ array声明,之前在循环中声明了$ events数组。所以我正在更新新代码
$events = array(); ## Declare the new array.
while ( $query->have_posts() ) : $query->the_post();
$mytime = get_post_meta($post->ID, 'wpcf-start-date', true);
$mydate = date("Y-m-D",$mytime);
$mydatearr = getdate($mytime);
$myday = $mydatearr['mday'];
$mymonth = $mydatearr['mon'];
$myyear = $mydatearr['year'];
$mytitle = $post->post_title;
if (($myyear == $year) && ($mymonth == $month))
$array = array('title' => $mytitle, 'event_date' => $myyear."-".$mymonth."-".$myday);
foreach ($array as $line) {
$event_date = $line['event_date']; ## Set event_date to a variable
$events[$event_date] = array( 'title' => $line['title'], 'event_date' => $event_date ); ###add new value to new array with key value
}
var_dump($events);
endwhile;
$ array的var_dump给出了
NULL
Warning: Invalid argument supplied for foreach() in /home/newmedia/public_html/dev/wp-gallerific/wp-content/themes/lajcc-theme/calendar.php on line 165
array(2) { ["title"]=> string(10) "Test Event" ["event_date"]=> string(9) "2012-8-24" } array(2) { ["title"]=> string(25) "RLI Class #42 Graduation " ["event_date"]=> string(9) "2012-8-16" }
$ event输出的var_dump
Warning: Invalid argument supplied for foreach() in /home/newmedia/public_html/dev/wp-gallerific/wp-content/themes/lajcc-theme/calendar.php on line 165
array(0) { } array(2) { ["T"]=> array(2) { ["title"]=> string(1) "T" ["event_date"]=> string(1) "T" } [2]=> array(2) { ["title"]=> string(1) "2" ["event_date"]=> string(1) "2" } } array(3) { ["T"]=> array(2) { ["title"]=> string(1) "T" ["event_date"]=> string(1) "T" } [2]=> array(2) { ["title"]=> string(1) "2" ["event_date"]=> string(1) "2" } ["R"]=> array(2) { ["title"]=> string(1) "R" ["event_date"]=> string(1) "R" } }
答案 0 :(得分:0)
我认为您需要通过数组foreach()
来获得所需的输出。
尝试一下:
$events = array(); ## Declare the new array.
foreach ($array as $line) {
$event_date = $line['event_date']; ## Set event_date to a variable
$events[$event_date] = array( 'title' => $line['title'], 'event_date' => $event_date ); ###add new value to new array with key value
}
var_dump($events); ##display the reults
祝你好运!
答案 1 :(得分:0)
你为什么要执行
$events = array(); ## Declare the new array.
在while
内?每次迭代基本上擦除先前迭代的所有数据集。只需将该行放在while
上方(外部,上方一个)和var_dump($events)
之后endwhile
。