如何在php中修改json_encode函数文件?

时间:2018-08-28 07:58:38

标签: javascript php jquery json mysqli

代码:

<script>
      $(document).ready(function(){
        var events = <?php echo $results; ?>;
        $("#calen").datepicker({
            beforeShowDay: function(date) {
                var result = [true, '', null];
                var matching = $.grep(events, function(event) {
                    return event.Date.valueOf() === date.valueOf();
                });

                if (matching.length) {
                    result = [true, 'highlight', null];
                }
                return result;
            },
            onSelect: function(dateText) {
                var date,
                    selectedDate = new Date(dateText),
                    i = 0,
                    event = null;

                while (i < events.length && !event) {
                    date = events[i].Date;

                    if (selectedDate.valueOf() === date.valueOf()) {
                        event = events[i];
                    }
                    i++;
                }
                if (event) {
                    alert(event.Title);
                }
            }
        });
      });
</script>

<?php
    $sqls = mysqli_query($con,"select pack_program, pack_duration, start_date, end_date from package_detail where pack_name = '".$row['pack_name']."'");
    while($fetch = mysqli_fetch_array($sqls))
    {
        $data[] = array(
                            'Title' => $fetch['pack_program'],
                            'Duration' => $fetch['pack_duration'],
                            'Date' => "new Date(".$fetch['start_date'].")"
                        );
    }
    $results = str_replace('</', '<\/', json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
?>

预期输出:

var events = [{Title:"Tour Program : Delhi / Chandigargh Arrival - Shimla ( 02 Nights ) – Manali ( 03 nights ) – Chandigarh ( 01 night ) Delhi / Chandigargh Departure Total : 06 Nights & 07 Days Tour",Duration:"7 days 6 Nights",Date:new Date("08/28/2018")}];

意外的输出:

var events = [{"Title":"Tour Program : Delhi / Chandigargh Arrival - Shimla ( 02 Nights ) – Manali ( 03 nights ) – Chandigarh ( 01 night ) Delhi / Chandigargh Departure Total : 06 Nights & 07 Days Tour","Duration":"7 days 6 Nights","Date":"new Date(08/28/2018)"}];

在下面的代码中,我已经使用jQuery datepicker创建了一个事件日历,如上所述,它对于expected output可以正常工作。但这不适用于上述unexpected output。因此,我该如何在$data[]文件中进行修改,这给了我expected output。我怎样才能做到这一点?请帮助我。

谢谢

2 个答案:

答案 0 :(得分:2)

您需要的是JSON.parse()

var events = <?php echo $results; ?>;
var obj = JSON.parse(events);

要了解更多信息-> here

答案 1 :(得分:0)

作为您的预期输出,new Date("08/28/2018")将成为Date对象,但在意外输出中,它是一个字符串。

让您尝试将代码更改为:

$data[] = array(
                        'Title' => $fetch['pack_program'],
                        'Duration' => $fetch['pack_duration'],
                        'Date' => date('Y-m-d', $fetch['start_date'])
                    );

输出将是:

var events = [{"Title":"Tour Program : Delhi / Chandigargh Arrival - Shimla ( 02 Nights ) – Manali ( 03 nights ) – Chandigarh ( 01 night ) Delhi / Chandigargh Departure Total : 06 Nights & 07 Days Tour","Duration":"7 days 6 Nights","Date":"2018-08-28"}];

,它将与datepicker

一起使用