2012-08-17 101 views
0

我正在使用FullCalendar jquery插件在我的网站上显示日历。我能够对日历中显示的任何值进行硬编码。格式应该是:为json格式化数组

echo json_encode(array(

     array(
      'id' => 1136, 
      'title' => "Understanding Health-Care Regulations (Part II) and COBRA Compliance Strategies", 
      'start' => "2011-11-17", 
      'url' => "/www/conferences/conference.php?ID=1136" 
     ), 

     array(
      'id' => 1154, 
      'title' => "Making the Most of your Membership", 
      'allDay' => false, 
      'start' => "Wed, 18 Nov 2011 11:00:00 EST", 
      'url' => "/www/conferences/conference.php?ID=1154" 
     ), 
     array(
      'id' => 1137, 
      'title' => "2011 Annual Human Resources Conference", 
      'start' => "2011-11-29", 
      'url' => "/www/conferences/conference.php?ID=1137" 
     ), 


    )); 

当试图模仿这种阵列结构,我使用的是这样的:

$conferences = dbStoredProc('cp_meeting_get_list_new'); 
$events = array(); 

foreach ($conferences as $c){ 
    $push = array(
     'id'  => $c['ID'], 
     'title'  => $c['name'], 
     'start'  => date("Y", $c['epochDate']) . "-" . date("M", $c['epochDate']) . "-" . date("d", $c['epochDate']), 
     'url'  => '/events/details.php?id' . $c['ID'], 
    ); 
    array_push($push, $events); 
} 
echo json_encode($events); 

当我赞同我的$events可变我得到[]

任何想法?

回答

6
array_push($push, $events); 

应该

array_push($events, $push); 

或只是

$events[] = $push; 
1

你最好不要只用

$events[] = $push; 

它的速度更快把数据附加到阵列,少混乱比不得不查找o参数。

0

您的PHP存在缺陷。试试这个:

$conferences = dbStoredProc('cp_meeting_get_list_new'); 
$events = array(); 

foreach ($conferences as $c){ 
    $events[] = array(
     'id'  => $c['ID'], 
     'title'  => $c['name'], 
     'start'  => date("Y-M-d", $c['epochDate']), 
     'url'  => '/events/details.php?id' . $c['ID'] 
    ); 
} 
echo json_encode($events); 
1

正如xdazz说,你需要的参数切换到array_push。可替代地,使用[]语法到物品推到所述阵列的所述端:

$events[] = $push; 

此外,还可以将多个格式说明date,所以你的启动线可被写为:

date("Y-M-d", $c['epochDate']),