1

为了有一个像这样的数组输出

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" } 

和 $events 输出的 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" } }  
4

2 回答 2

0

你为什么要执行

$events = array(); ## Declare the new array.

在你的里面while?每次迭代基本上都会擦除先前迭代中的所有数据集。只需将那条线放在你的上方while(外面,一个住在上面)和var_dump($events)你的endwhile.

于 2012-08-12T00:11:52.440 回答
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

祝你好运!

于 2012-08-11T23:32:23.700 回答