-1
$cnt = 0;
while ($row = $result->fetch_assoc()) 
{
    $arre[$cnt]['id'] = $row['idevents'];
    $arre[$cnt]['title'] = $row['title'];
    $arre[$cnt]['start'] = "new Date(" . $row['start'] . "*1000)";
    $arre[$cnt]['end'] = "new Date(" . $row['end'] . "*1000)";
    $arre[$cnt]['allDay'] = $row['allday'];
    $arre[$cnt]['url'] = $row['url'];
    $cnt++;
}


$year = date('Y');
$month = date('m');

echo json_encode(array(

array(
    'id' => 111,
    'title' => "Event1",
    'start' => "$year-$month-10",
    'url' => "http://yahoo.com/"
),

array(
    'id' => 222,
    'title' => "Event2",
    'start' => "$year-$month-20",
    'end' => "$year-$month-22",
    'url' => "http://yahoo.com/"
)

));

?>

脚本底部的 json_encode 是一个示例。我需要获取 $arre 和 json_encode 中的数据。json_encode 的格式需要保持几乎完全相同,否则程序可能会觉得它不好吃,我的程序将无法运行。有谁知道正确的代码技术在这里是什么样的?

谢谢你!

4

1 回答 1

1

如果您正在寻找正确的格式来使用 json_encode() 将数组返回给您的函数,这里有一个示例。使用键值对访问不同的成员:

此外,使用关联数组,以便您可以通过列名而不是整数值来迭代客户端上的元素。

while ($row = $result->fetch_assoc()) 
{
    $thisRow = array(
                     'id'     => $row['idevents'],
                     'title'  => $row['title'],
                     'start'  => date("F j, Y, g:i a", strtotime($row['start'])),
                     'end'    => date("F j, Y, g:i a", strtotime($row['end'])),
                     'allDay' => $row['allday'],
                     'url'    => $row['url']
    );
    array_push($arre, $thisRow);
}

return json_encode(
    array(
        "result" => "success", 
        "data" => $arre
    )
);

然后在你的 javascript/jquery 中:

$.post("myPost.php", post_data,
    function(data) {
        // store data.result;
        // store data.data;
    }, 
"json");
于 2012-12-12T23:26:16.930 回答