删除JSON文件和PHP的最后一个逗号

时间:2015-07-09 14:11:36

标签: php json

我有一个带有PHP的JSON文件,我想删除最后一个逗号,因为它会导致错误。代码如下:

{
"data": [
<?php
    require_once("../../config.php");
    $qtodos = $mysqli->query("SELECT * FROM negocios");
    while($todos = $qtodos -> fetch_assoc()) {
        ?>
        {
            "id": <?php echo $todos['idnegocios']; ?>,
            "category": "real_estate",
            "title": "<?php echo $todos['nombre']; ?>",
            "location": "<?php echo $todos['direccion']; ?>",
            "latitude": 51.541599,
            "longitude": -0.112588,
            "url": "item-detail.html",
            "type": "Apartment",
            "type_icon": "assets/icons/store/apparel/umbrella-2.png",
            "rating": 4,
            "gallery":
                [
                    "assets/img/items/1.jpg",
                    "assets/img/items/5.jpg",
                    "assets/img/items/4.jpg"
                ],
            "features":
                [
                    "Free Parking",
                    "Cards Accepted",
                    "Wi-Fi",
                    "Air Condition",
                    "Reservations",
                    "Teambuildings",
                    "Places to seat"
                ],
            "date_created": "2014-11-03",
            "price": "$2500",
            "featured": 0,
            "color": "",
            "person_id": 1,
            "year": 1980,
            "special_offer": 0,
            "item_specific":
                {
                    "bedrooms": 4,
                    "bathrooms": 2,
                    "rooms": 4,
                    "garages": 1,
                    "area": 240
                },
            "description": "asasasas odio nibh, luctus non pulvinar a, ultricies ac diam. Donec neque massa, viverra interdum eros ut, imperdiet pellentesque mauris. Proin sit amet scelerisque risus. Donec semper semper erat ut mollis. Curabitur suscipit, justo eu dignissim lacinia, ante sapien pharetra duin consectetur eros augue sed ex. Donec a odio rutrum, hendrerit sapien vitae, euismod arcu.",
            "last_review": "Curabitur odio nibh, luctus non pulvinar a, ultricies ac diam. Donec neque massa, viverra interdum eros ut, imperdiet",
            "last_review_rating": 5
        },
<?php
}
?>
    ]
}

我尝试了很多解决方案,但从未使用过此代码。我不知道它何时是循环的最后一个循环,并且无法将其删除。

4 个答案:

答案 0 :(得分:1)

许多可能的方式。我只选了一个....
删除尾随的文字逗号并像这样更改脚本

$first = true;
while($todos = $qtodos -> fetch_assoc()) {
    if ($first) {
        $first = false;
    }
    else {
        echo ',';
    }
?>
        {
            "id":

即。脚本在记录之后但在下一条记录之前不打印逗号。

答案 1 :(得分:1)

获取查询行计数并创建一个计数器以了解您在循环中迭代的行,只有在计数器与总行数不同时才添加逗号:

  {
    "data": [
    <?php
        require_once("../../config.php");
        $qtodos = $mysqli->query("SELECT * FROM negocios");
        $TotalRcount = $qtodos->num_rows; // <<<< row count
        $counter = 0;

        while($todos = $qtodos -> fetch_assoc()) { $counter++ // <<<< add the counter
            ?>
            {
                "id": <?php echo $todos['idnegocios']; ?>,
                "category": "real_estate",
                "title": "<?php echo $todos['nombre']; ?>",
                "location": "<?php echo $todos['direccion']; ?>",
                "latitude": 51.541599,
                "longitude": -0.112588,
                "url": "item-detail.html",
                "type": "Apartment",
                "type_icon": "assets/icons/store/apparel/umbrella-2.png",
                "rating": 4,
                "gallery":
                    [
                        "assets/img/items/1.jpg",
                        "assets/img/items/5.jpg",
                        "assets/img/items/4.jpg"
                    ],
                "features":
                    [
                        "Free Parking",
                        "Cards Accepted",
                        "Wi-Fi",
                        "Air Condition",
                        "Reservations",
                        "Teambuildings",
                        "Places to seat"
                    ],
                "date_created": "2014-11-03",
                "price": "$2500",
                "featured": 0,
                "color": "",
                "person_id": 1,
                "year": 1980,
                "special_offer": 0,
                "item_specific":
                    {
                        "bedrooms": 4,
                        "bathrooms": 2,
                        "rooms": 4,
                        "garages": 1,
                        "area": 240
                    },
                "description": "asasasas odio nibh, luctus non pulvinar a, ultricies ac diam. Donec neque massa, viverra interdum eros ut, imperdiet pellentesque mauris. Proin sit amet scelerisque risus. Donec semper semper erat ut mollis. Curabitur suscipit, justo eu dignissim lacinia, ante sapien pharetra duin consectetur eros augue sed ex. Donec a odio rutrum, hendrerit sapien vitae, euismod arcu.",
                "last_review": "Curabitur odio nibh, luctus non pulvinar a, ultricies ac diam. Donec neque massa, viverra interdum eros ut, imperdiet",
                "last_review_rating": 5
            }<?php
            if($TotalRcount != $counter)
            { echo ','};  // <<<< only add the comma when the counter is different than total rows
    }
    ?>
        ]
    }

答案 2 :(得分:0)

你可以添加一个计数并将计数与总行数相匹配,这样当它到达最后一条记录时它就不会回显逗号

{
"data": [
<?php
    require_once("../../config.php");
    $qtodos = $mysqli->query("SELECT * FROM negocios");
    $count = 1;
    while($todos = $qtodos -> fetch_assoc()) {
        ?>
        {
            "id": <?php echo $todos['idnegocios']; ?>,
            "category": "real_estate",
            "title": "<?php echo $todos['nombre']; ?>",
            "location": "<?php echo $todos['direccion']; ?>",
            "latitude": 51.541599,
            "longitude": -0.112588,
            "url": "item-detail.html",
            "type": "Apartment",
            "type_icon": "assets/icons/store/apparel/umbrella-2.png",
            "rating": 4,
            "gallery":
                [
                    "assets/img/items/1.jpg",
                    "assets/img/items/5.jpg",
                    "assets/img/items/4.jpg"
                ],
            "features":
                [
                    "Free Parking",
                    "Cards Accepted",
                    "Wi-Fi",
                    "Air Condition",
                    "Reservations",
                    "Teambuildings",
                    "Places to seat"
                ],
            "date_created": "2014-11-03",
            "price": "$2500",
            "featured": 0,
            "color": "",
            "person_id": 1,
            "year": 1980,
            "special_offer": 0,
            "item_specific":
                {
                    "bedrooms": 4,
                    "bathrooms": 2,
                    "rooms": 4,
                    "garages": 1,
                    "area": 240
                },
            "description": "asasasas odio nibh, luctus non pulvinar a, ultricies ac diam. Donec neque massa, viverra interdum eros ut, imperdiet pellentesque mauris. Proin sit amet scelerisque risus. Donec semper semper erat ut mollis. Curabitur suscipit, justo eu dignissim lacinia, ante sapien pharetra duin consectetur eros augue sed ex. Donec a odio rutrum, hendrerit sapien vitae, euismod arcu.",
            "last_review": "Curabitur odio nibh, luctus non pulvinar a, ultricies ac diam. Donec neque massa, viverra interdum eros ut, imperdiet",
            "last_review_rating": 5
        }
<?php
    if ((int)$qtodos->num_rows != $count)
        echo ',';
    $count++;
}
?>
    ]
}

答案 3 :(得分:-2)

Hope, this will work 
`{
"data": [
<?php
    require_once("../../config.php");
    $qtodos = $mysqli->query("SELECT * FROM negocios");
    $count = 0;
    while($todos = $qtodos -> fetch_assoc()) {
        ?>
        {
            "id": <?php echo $todos['idnegocios']; ?>,
            "category": "real_estate",
            "title": "<?php echo $todos['nombre']; ?>",
            "location": "<?php echo $todos['direccion']; ?>",
            "latitude": 51.541599,
            "longitude": -0.112588,
            "url": "item-detail.html",
            "type": "Apartment",
            "type_icon": "assets/icons/store/apparel/umbrella-2.png",
            "rating": 4,
            "gallery":
                [
                    "assets/img/items/1.jpg",
                    "assets/img/items/5.jpg",
                    "assets/img/items/4.jpg"
                ],
            "features":
                [
                    "Free Parking",
                    "Cards Accepted",
                    "Wi-Fi",
                    "Air Condition",
                    "Reservations",
                    "Teambuildings",
                    "Places to seat"
                ],
            "date_created": "2014-11-03",
            "price": "$2500",
            "featured": 0,
            "color": "",
            "person_id": 1,
            "year": 1980,
            "special_offer": 0,
            "item_specific":
                {
                    "bedrooms": 4,
                    "bathrooms": 2,
                    "rooms": 4,
                    "garages": 1,
                    "area": 240
                },
            "description": "asasasas odio nibh, luctus non pulvinar a, ultricies ac diam. Donec neque massa, viverra interdum eros ut, imperdiet pellentesque mauris. Proin sit amet scelerisque risus. Donec semper semper erat ut mollis. Curabitur suscipit, justo eu dignissim lacinia, ante sapien pharetra duin consectetur eros augue sed ex. Donec a odio rutrum, hendrerit sapien vitae, euismod arcu.",
            "last_review": "Curabitur odio nibh, luctus non pulvinar a, ultricies ac diam. Donec neque massa, viverra interdum eros ut, imperdiet",
            "last_review_rating": 5
        }
    if($count != mysql_num_rows($todos))
    {
        ,
    }
    $count += 1;
<?php
}
?>
    ]
}`