PHP Array到JSON格式的对象

时间:2013-02-20 00:35:26

标签: php json

我想我需要另外一双眼来审查这段代码。我无法获得正确的数组语句组合来获取我想要的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
                 }
              ]
           ]
        }
     ]
  }
 ]
 }

0 个答案:

没有答案