Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何通过php将多个数据库记录正确转换为json_Php - Fatal编程技术网

如何通过php将多个数据库记录正确转换为json

如何通过php将多个数据库记录正确转换为json,php,Php,我有手动构建的json记录,在该记录下,我的应用程序可以正常工作 <?php $arr= '{ "items": [{ "title": "my first title", "id": "101", "address": { "label": "my first la

我有手动构建的json记录,在该记录下,我的应用程序可以正常工作

<?php 

$arr= '{
"items": [{
        "title": "my first title",
        "id": "101",
        "address": {
            "label": "my first label",
            "countryCode": "FRA"
        },
        "position": {
            "lat": 37.37634,
            "lng": -122.03405
        }
    },

    {
        "title": "my second title",
        "id": "102",
        "address": {
            "label": "my second label",
            "countryCode": "USA"
        },
        "position": {
            "lat": 37.37634,
            "lng": -122.03405
        }
    }
]
}';

echo $arr;    
?>

地址{}和位置{}应为数组:

$arr[] = array(
    "id" => $id,    
    "title" => $title,    
    "address" => [
        "label" => $label,
        "countryCode" => $countryCode,
    ],
    "position" => [
        "lat" => $lat,
        "lng" => $lng,
    ],
);

地址{}和位置{}应为数组:

$arr[] = array(
    "id" => $id,    
    "title" => $title,    
    "address" => [
        "label" => $label,
        "countryCode" => $countryCode,
    ],
    "position" => [
        "lat" => $lat,
        "lng" => $lng,
    ],
);

只要创建一个数组或一个对象(如果您愿意),并将属于它们的内容放入其中,然后将其放入您拥有的数组中

while($row = $res1->fetch()){

    $title = htmlentities(htmlentities($row['title'], ENT_QUOTES, "UTF-8"));
    $id = htmlentities(htmlentities($row['id'], ENT_QUOTES, "UTF-8"));
    
    $addr = [];
    $addr['label']        = htmlentities(htmlentities($row['label'], ENT_QUOTES, "UTF-8"));
    $addr['countryCode']  = htmlentities(htmlentities($row['countryCode'], ENT_QUOTES, "UTF-8"));

    $pos = [];
    $pos['lat'] = htmlentities(htmlentities($row['lat'], ENT_QUOTES, "UTF-8"));
    $pos['lng'] = htmlentities(htmlentities($row['lng'], ENT_QUOTES, "UTF-8"));

    $arr[] = array(
            "id" => $id,
            "title" => $title,
            "address" => $addr,
            "position" => $pos
        );
}
echo json_encode(["items"=>$arr]);

只要创建一个数组或一个对象(如果您愿意),并将属于它们的内容放入其中,然后将其放入您拥有的数组中

while($row = $res1->fetch()){

    $title = htmlentities(htmlentities($row['title'], ENT_QUOTES, "UTF-8"));
    $id = htmlentities(htmlentities($row['id'], ENT_QUOTES, "UTF-8"));
    
    $addr = [];
    $addr['label']        = htmlentities(htmlentities($row['label'], ENT_QUOTES, "UTF-8"));
    $addr['countryCode']  = htmlentities(htmlentities($row['countryCode'], ENT_QUOTES, "UTF-8"));

    $pos = [];
    $pos['lat'] = htmlentities(htmlentities($row['lat'], ENT_QUOTES, "UTF-8"));
    $pos['lng'] = htmlentities(htmlentities($row['lng'], ENT_QUOTES, "UTF-8"));

    $arr[] = array(
            "id" => $id,
            "title" => $title,
            "address" => $addr,
            "position" => $pos
        );
}
echo json_encode(["items"=>$arr]);