为两个表生成单个JSON文件

时间:2016-05-13 10:52:29

标签: php json

我正在尝试创建一个显示来自两个不同表的数据的JSON文件。我可以设法获取它们,但是我不知道如何连接这两个数组。我是JSON的新手。任何建议/教程都会非常有帮助。提前谢谢。

这里是预期的JSON:

    {
"array1":[
{"days":"1","id":"1","image":"image1.jpg, image2.jpg, image3.jpg, image4.jpg"},{"days":"2","id":"1","image":"elephanta.jpg,image2.jpg,image1.jpg,imagica.jpg"},
{"days":"3","id":"1","image":"image3"},{"days":"4","id":"2","image":"image4"}
],
"array2":[
{"id":"1","image_1":"image1.jpg"},
{"id":"2","image_1":"image2.jpg"},
{"id":"3","image_1":"image3.jpg"}
]
}

但我得到的JSON是这样的:

{
"array1":[
{"days":"1","id":"1","image":"image1.jpg, image2.jpg, image3.jpg, image4.jpg"},{"days":"2","id":"1","image":"elephanta.jpg,image2.jpg,image1.jpg,imagica.jpg"},
{"days":"3","id":"1","image":"image3"},{"days":"4","id":"2","image":"image4"}
]},
{"array2":[
{"id":"1","image_1":"image1.jpg"},
{"id":"2","image_1":"image2.jpg"},{"id":"3","image_1":"image3.jpg"
}
]}

PHP代码:

<?php
    //open connection to mysql db
    $connection = mysqli_connect("localhost","root","","test") or die("Error " . mysqli_error($connection));

    //fetch table rows from mysql db
    $sql = "select * from image_data";
    $sql1 = "select * from one";
    $result = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection));
    $result1 = mysqli_query($connection, $sql1) or die("Error in Selecting " . mysqli_error($connection));

    //create an array
    $imageArray = array();
    $imageArray1 = array();
    $imageArray["array1"] = array();
    $imageArray1["array2"] = array();
    while($row =mysqli_fetch_array($result))
    {
        $tmp = array();
        $tmp["days"] = $row["id"];
        $tmp["id"] =   $row["place_id"];
        $tmp["image"] = $row["image"];
        array_push($imageArray["array1"], $tmp);
       // $imageArray[] = $row;
    }

        while($row =mysqli_fetch_array($result1))
    {
        $tmp = array();
        $tmp["id"] = $row["id"];
        $tmp["image_1"] = $row["image"];

        array_push($imageArray1["array2"], $tmp);
       // $imageArray[] = $row;
    }
            echo json_encode($imageArray);
  echo ",";
  echo json_encode($imageArray1);
    //close the db connection
    mysqli_close($connection);
?>

1 个答案:

答案 0 :(得分:1)

如果你想让2个子数组属于同一个数组,那么在加载它们时只使用一个数组并像这样处理2个子数组

$imageArray["array1"][] = $tmp;

$imageArray["array2"][] = $tmp;

修改后的代码

<?php
    //open connection to mysql db
    $connection = mysqli_connect("localhost","root","","test") or die("Error " . mysqli_error($connection));

    //fetch table rows from mysql db
    $sql = "select * from image_data";
    $sql1 = "select * from one";
    $result = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection));
    $result1 = mysqli_query($connection, $sql1) or die("Error in Selecting " . mysqli_error($connection));

    //create an array
    $imageArray = array();       // <- removed other arrays

    while($row =mysqli_fetch_array($result))
    {
        $tmp = array();
        $tmp["days"] = $row["id"];
        $tmp["id"] =   $row["place_id"];
        $tmp["image"] = $row["image"];
        $imageArray["array1"][] = $tmp;    // <- address array like this
    }

    while($row =mysqli_fetch_array($result1))
    {
        $tmp = array();
        $tmp["id"] = $row["id"];
        $tmp["image_1"] = $row["image"];

        $imageArray["array2"][] = $tmp;    // <- address array like this
    }
    echo json_encode($imageArray);         // <- only one json_encode

    //close the db connection
    mysqli_close($connection);
?>

我会这样做,使用stdObject并从表中选择特定数据,然后使用mysqli_fetch_obect(),这样您就可以做的事情少得多woman-draulically

<?php
    //open connection to mysql db
    $connection = mysqli_connect("localhost","root","","test") or die("Error " . mysqli_error($connection));

    //fetch table rows from mysql db
    // only select what you want
    $sql = "select id,place_id,image from image_data";
    $sql1 = "select id,image from one";

    $result = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection));
    $result1 = mysqli_query($connection, $sql1) or die("Error in Selecting " . mysqli_error($connection));

    //create an array
    $imageObject = new stdObject();

    while($row =mysqli_fetch_object($result))
    {
        $imageObject->array1[] = $row;
    }

    while($row =mysqli_fetch_object($result1))
    {
        $imageObject->array2[] = $row;
    }

    echo json_encode($imageObject);
?>