0

我想我需要另一双眼睛来审查这段代码。我无法获得正确的数组语句组合来获得我想要的 JSON 对象。

    $sql = "SELECT categories.category_name AS category_id, sub_categories.sub_category_name AS subcategory_id, items.id AS item_id, items.item_name
            FROM categories
            LEFT JOIN sub_categories ON (categories.id = sub_categories.category_id)
            LEFT JOIN items ON (sub_categories.id = items.sub_category_id)
            WHERE categories.site_id = 1
            ORDER BY category_id,subcategory_id";


    $result1 = mysqli_query ($dbc, $sql) or trigger_error("Query: $sql\n<br />MySQL Error: " . mysqli_error($dbc));

    if($result1 === FALSE)
    {
        echo(mysqli_error()); // TODO: better error handling
    } else
    {



    $tempCategoryArray = array();
    $tempSubCategoryArray = array();

    $categoriesArray = array("categories" => array());
    $category = "";
    $sub_category = "";
    $newCat= FALSE;
    $newSubCat = FALSE;

    while($row = mysqli_fetch_array($result1)) 


        {

        // If we have a new category, let's start anew

        if($row['category_id'] != $category) 
                {

                $tempCategoryArray['category_id'][]= $row['category_id'];

                // Add the temporary array to the main one
                    if(!empty($tempCategoryArray)) 
                        {
                       array_push ($categoriesArray['categories'], $tempCategoryArray);

                        }


                    $tempCategoryArray = array();


                    $tempCategoryArray['subcategories'] = array();

                }


                $category = $row['category_id'];
        // Same here, if a new sub, let's start fresh

        if($row['subcategory_id'] != $sub_category) 
                {

                $tempSubCategoryArray['subcategory_id'][] = $row['subcategory_id'];
                $tempSubCategoryArray['items'] = array(); 
                        // Add it to the tempCategory
                    if(!empty($tempSubCategoryArray)) 
                        {
                        array_push   ($tempCategoryArray['subcategories'], $tempSubCategoryArray);


                        }




                }

                 { 
                   $tempSubCategoryArray['items'] = array(); 


                      array_push($tempSubCategoryArray['items'],array('item_name'=>     $row['item_name'], 'item_id'=> $row['item_id'], 'item_qty'=> 0));
                       array_push($tempCategoryArray,$tempSubCategoryArray['items']);



                      unset($tempSubCategoryArray);

                 }

        // Finally, no need for temporary arrays with items, since they have no sub-    array

                $sub_category= $row['subcategory_id'];


        }
    array_push ($categoriesArray['categories'], $tempCategoryArray);
    }

    //var_dump($categoriesArray);
    $categoryJson = json_encode($categoriesArray);

    echo $categoryJson;

    //echo '<script  src="js/menupages.js"></script>';


    // Clean up:
    mysqli_free_result($result1);

    ?>

这是我在进行更改时得到的对象及其变体,但我无法将其传递给我的目标 JSON 对象

当前 JSON

{
"categories":[
  {
     "category_id":[
        "Drinks"
     ]
  },
  {
     "subcategories":[
        {
           "subcategory_id":[
              "Beer"
           ],
           "items":[

           ]
        },
        {
           "subcategory_id":[
              "Wine"
           ],
           "items":[

           ]
        }
     ],
     "0":[
        {
           "item_name":"Miller   Lite",
           "item_id":"5",
           "item_qty":0
        }
     ],
     "1":[
        {
           "item_name":"Yuengling",
           "item_id":"6",
           "item_qty":0
        }
     ],
     "2":[
        {
           "item_name":"Bud Select",
           "item_id":"7",
           "item_qty":0
        }
     ],
     "3":[
        {
           "item_name":"White Zin",
           "item_id":"8",
           "item_qty":0
        }
     ],
     "4":[
        {
           "item_name":"Reistling",
           "item_id":"10",
           "item_qty":0
        }
     ],
     "category_id":[
        "Food"
     ]
  },
  {
     "subcategories":[
        {
           "subcategory_id":[
              "Sandwiches"
           ],
           "items":[

           ]
        }
     ],
     "0":[
        {
           "item_name":"Hamburger",
           "item_id":"9",
           "item_qty":0
        }
     ],
     "1":[
        {
           "item_name":"Hot Dog",
           "item_id":"11",
           "item_qty":0
        }
     ]
  }
]
}

我真正想要的 JSON

{
"categories":[
  {
     "category_id":[
        "Drinks"
     ]
  },
  {
     "subcategories":[
        {
           "subcategory_id":[
              "Beer"
           ],
           "items":[
              [
                 {
                    "item_name":"Miller Lite",
                    "item_id":"5",
                    "item_qty":0
                 }
              ],
              [
                 {
                    "item_name":"Yuengling",
                    "item_id":"6",
                    "item_qty":0
                 }
              ],
              [
                 {
                    "item_name":"Bud Select",
                    "item_id":"7",
                    "item_qty":0
                 }
              ]
           ]
        },
        {
           "subcategory_id":[
              "Wine"
           ],
           "items":[
              [
                 {
                    "item_name":"White Zin",
                    "item_id":"8",
                    "item_qty":0
                 }
              ],
              [
                 {
                    "item_name":"Reistling",
                    "item_id":"10",
                    "item_qty":0
                 }
              ]
           ]
        }
     ],
     "category_id":[
        "Food"
     ]
  },
  {
     "subcategories":[
        {
           "subcategory_id":[
              "Sandwiches"
           ],
           "items":[
              [
                 {
                    "item_name":"Hamburger",
                    "item_id":"9",
                    "item_qty":0
                 }
              ],
              [
                 {
                    "item_name":"Hot Dog",
                    "item_id":"11",
                    "item_qty":0
                 }
              ]
           ]
        }
     ]
  }
 ]
 }
4

0 回答 0