PHP中JSON的数组问题

时间:2013-07-21 04:59:32

标签: php arrays json

我正在尝试在PHP中输出JSON提要,并且我一直遇到一个问题,即JSON提要中的第二个结果也包含第一个结果。

下面的来源和输出:

来源

function fetch_tour_list($tourID) {
    include('../inc/conn.php');

    $query = mysqli_query($conn,"SELECT * FROM ticket_tour_dates WHERE TourID = $tourID");
    while($result = mysqli_fetch_array($query)) {

        $date['date'] = $result['Date'];
        $venueID = $result['VenueID'];

            $venue_query = mysqli_query($conn,"SELECT * FROM ticket_venues WHERE ID = $venueID");
            while($venue_result = mysqli_fetch_array($venue_query)) {
                $venue['id'] = $venue_result['ID'];
                $venue['name'] = $venue_result['Name'];
                $venue['location'] = $venue_result['Location'];
                $venue['latitude'] = $venue_result['Lat'];
                $venue['longitude'] = $venue_result['Long'];

                $venues[] = $venue;
            }


        $date['venue'] = $venues;
        $dates[] = $date;

    }

    echo json_encode($dates);
    mysqli_close($conn);
}

输出

[{"date":"2013-07-29","venue":[{"id":"1","name":"The Gramercy","location":"New York City","latitude":"50.00000000","longitude":"50.00000000"}]},{"date":"2013-08-02","venue":[{"id":"1","name":"The Gramercy","location":"New York City","latitude":"50.00000000","longitude":"50.00000000"},{"id":"2","name":"The Troubadour","location":"Chicago","latitude":"20.00000000","longitude":"25.00000000"}]}]

1 个答案:

答案 0 :(得分:2)

在内部while循环之前添加以下行:

$venues = array();