简体   繁体   English

使用PHP在JSON数组中创建数组

[英]Creating an array inside of an array for json using PHP

I am trying to replicate this json file using PHP but I cannot seem to get the array for object "location" within another array called "levels" 我正在尝试使用PHP复制此json文件,但似乎无法在另一个名为“级别”的数组中获取对象“位置”的数组

"levels": [
    {
        "id": "lower",
        "title": "Floor 26",
        "map": "images/apartment/upper.jpg",
        "minimap": "images/apartment/upper-small.png",
        "locations": [

            {
                "id": "1",
                "title": "Kitchen",
                "about": "",
                "description": "<img src=\"images/logo.png\" 
                 alt=\"sdfsfs\"><p>aaaaaaaaaaaaaa<p>",
                "category": "furniture",
                "x": "0.4746",
                "y": "0.2883",
                "zoom": "1"
            },

This is what I have so far. 到目前为止,这就是我所拥有的。

$connect1 = new mysqli("localhost", "root", "root", "location");
$result1 = mysqli_query($connect1, "SELECT * FROM locations");

$connect2 = new mysqli("localhost", "root", "root", "Floors");
$result2 = mysqli_query($connect2, "SELECT * FROM levels");


$data = array(); 
$levels = array();

while($row = mysqli_fetch_array($result2, MYSQLI_ASSOC)){
    $levels[] = $row;
}

  $data["levels"] = $levels;   

  $locations = array();


    while($row = mysqli_fetch_array($result1, MYSQLI_ASSOC)){
    $locations[] = $row;
    }

    $data["locations"] = $locations;

This is the output 这是输出

"levels": [
    {
        "id":"1",
        "title":"Floor26",
        "map":"images\/apartment\/upper.jpg",
        "minimap":"images\/apartment\/upper-small.png"
    }
],
"locations": [
    {

The problem is I want locations to be an array within the category of levels as shown in the json file rather than levels array being closed after the data has been inserted. 问题是我希望locations是json文件中所示的级别类别中的数组,而不是在插入数据后关闭的levels数组。 Please help. 请帮忙。 I am a complete beginner, so any advice will be appreciated. 我是一个完整的初学者,因此任何建议将不胜感激。

First, use more descriptive variable names. 首先,使用更具描述性的变量名称。 They're easier to read and understand. 它们更易于阅读和理解。

Second, add the locations to the levels array in a nested loop: 其次,将位置添加到嵌套循环的levels数组中:


Edit: Actually, you don't need to nest the loops, since the locations are the same for every level. 编辑:实际上,您不需要嵌套循环,因为每个级别的位置都相同。 If you were querying for different locations each time, or changing which locations you imported based on level, nesting would make sense. 如果您每次都在查询不同的位置,或者根据级别更改导入的位置,则嵌套是有意义的。

$locationConnect = new mysqli("localhost", "root", "root", "location");
$locationsResult = mysqli_query($locationConnect, "SELECT * FROM locations");

$levelsConnect = new mysqli("localhost", "root", "root", "Floors");
$levelsResult = mysqli_query($levelsConnect, "SELECT * FROM levels");

$data = array(); 
$levels = array(); 
$locationsArray = array();

//actually since locations appear to be the same for all results, you don't need to nest the loops, this is more efficient:
//loop location results and build locations array:
while($locationRow = mysqli_fetch_array($locationResult, MYSQLI_ASSOC)){
    $locationsArray[] = $locationRow;
}

while($levelRow = mysqli_fetch_array($levelsResult, MYSQLI_ASSOC)){        
    //add locations array to your level row:
    $levelRow["locations"] = $locationsArray;
    //add level row to your levels array
    $levels[] = $levelRow;
}

$data["levels"] = $levels;  

//are you using this for something else? $employee_data seems to be a new array
$employee_data["locations"] = $locationsArray;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM