Php 为两个表生成单个JSON文件

Php 为两个表生成单个JSON文件,php,json,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

我试图创建一个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);
?>

如果希望这两个子数组属于同一个数组,则只使用一个数组,并在加载它们时对这两个子数组进行如下寻址

$imageArray["array1"][] = $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);
?>

而不是使用
array\u push($imageArray[“array1”],$tmp),为什么不试试:
$tmp[]=$imageArray[“array1”]?不是单独序列化每个数组并尝试手动构建JSON,创建一个包含两个数组的对象并序列化该对象。我使用了urs:D…尝试了前面的建议..但后来看到了ur post..它确实使用了D wrk:)。。。thanks@PoojaGangurde他很想知道这是第一个版本还是第二个版本?@RiggsFolly我只是做了一次尝试,每次我尝试,你先回答。。。Grrrrr…:PSorry@PraveenKumar只是想赶上你那令人印象深刻的代表hehehe@PraveenKumar我们就解决方案达成一致了吗?
<?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);
?>
<?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);
?>