2016-05-13 23 views
2

我想創建一個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); 
?> 
+0

而不是使用'array_push($ imageArray [「array1」],$ tmp);',爲什麼不試試:'$ tmp [] = $ imageArray [「array1」]);'? –

+0

而不是單獨序列化每個數組並嘗試*手動*構建JSON,則創建一個包含*兩個數組並對該對象進行序列化的對象。 – David

回答

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); 
?> 
+1

出於興趣,您使用了哪些代碼? – RiggsFolly

+0

謝謝@riggsfolly .... –

+0

我用urs:D ...正在嘗試早先的建議..但後來看到你的職位..它沒有d wrk:)...謝謝 –