PHP JSON格式问题

时间:2018-11-07 12:02:43

标签: php arrays json

我目前停留在php的json格式中。我在下面给出了我的输出json。我需要做的是使当前json的格式成为所需的格式。我缺少JSON格式的数组。谁能帮我这个忙。

我用于打印json输出的代码如下:

$menuHead=array(); 
$i=0; 
foreach($res as $key => $value){ 
    $i=$key+1;
    //$menuHead[$i]['menuHead']=$value['category'];
    if(isset($menuHead[$key]['menuHead'])){
        if($menuHead[$key]['menuHead']==$value['category']){
          $menuHead[$key]['data'][$i]['itemName']=$value['sub_category'];
          $menuHead[$key]['data'][$i]['price']=$value['price'];
          $menuHead[$key]['data'][$i]['description']=$value['description'];
          $menuHead[$key]['data'][$i]['itemId']=$value['id'];
          $menuHead[$key]['data'][$i]['customizable']=$value['customizable'];
        }else{
          $menuHead[$i]['menuHead']=$value['category'];
          $menuHead[$i]['data'][$i]['itemName']=$value['sub_category'];  
          $menuHead[$i]['data'][$i]['price']=$value['price'];
          $menuHead[$i]['data'][$i]['description']=$value['description'];
          $menuHead[$i]['data'][$i]['itemId']=$value['id'];
          $menuHead[$i]['data'][$i]['customizable']=$value['customizable'];
        }
    }else{
      $menuHead[$i]['menuHead']=$value['category'];
      $menuHead[$i]['data'][$i]['itemName']=$value['sub_category'];  
      $menuHead[$i]['data'][$i]['price']=$value['price'];
      $menuHead[$i]['data'][$i]['description']=$value['description'];
      $menuHead[$i]['data'][$i]['itemId']=$value['id'];
      $menuHead[$i]['data'][$i]['customizable']=$value['customizable'];
    }    
}
$final['MenuList']=$menuHead;
echo json_encode($final);

当前格式:

    {
  "MenuList": {
    "1": {
      "menuHead": "Main Course",
      "data": {
        "1": {
          "itemName": "Chicken Thai Curry",
          "price": "599",
          "description": "",
          "itemId": "67",
          "customizable": "1"
        }
      }
    },
    "2": {
      "menuHead": "Refreshments",
      "data": {
        "2": {
          "itemName": "Kingfisher Premium",
          "price": "999",
          "description": "Kingfisher beer",
          "itemId": "69",
          "customizable": "1"
        },
        "3": {
          "itemName": "Mocktail",
          "price": "999",
          "description": "",
          "itemId": "68",
          "customizable": "1"
        }
      }
    },
    "4": {
      "menuHead": "Rice biriyani",
      "data": {
        "4": {
          "itemName": "Dal makni risotto",
          "price": "499",
          "description": "Dal makhni risotto",
          "itemId": "66",
          "customizable": "1"
        }
      }
    }
  }
}

所需格式:

    {
  "menuList": [
    {
      "menuHead": "In Steamer (Momos)",
      "data": [
        {
          "itemName": "Tandoori Momo",
          "description": "",
          "price": "150",
          "itemId": "16",
          "customizable": "0"
        },
        {
          "itemName": "Fried Momo Pork",
          "price": "100",
          "description": "",
          "itemId": "15",
          "customizable": "0"
        }
      ]
    },
    {
      "itemName": "Rice and Noodles",
      "data": [
        {
          "sub_category": "Tandoori Momo",
          "description": "",
          "price": "150",
          "itemId": "16",
          "customizable": "0"
        },
        {
          "itemName": "Fried Momo Pork",
          "price": "100",
          "description": "",
          "itemId": "15",
          "customizable": "0"
        }
      ]
    }
  ]
}

原始响应如下:

array(4){[0] => array(7){[“ id”] => string(2)“ 67” [“ restaurant_id”] => string(1)“ 5” [“ category” ] =>字符串(11)“主菜” [“ sub_category”] =>字符串(18)“鸡肉泰式咖喱” [“价格”] =>字符串(3)“ 599” [“描述”] =>字符串( 0)“” [“” customizable“] =>字符串(1)” 1“} [1] =>数组(7){[”“ id”] =>字符串(2)“ 69” [“ restaurant_id”] => string(1)“ 5” [“ category”] => string(12)“小食” [“ sub_category”] => string(18)“ Kingfisher Premium” [“ price”] => string(3)“ 999” [“ description”] => string(15)“ Kingfisher beer” [“ customizable”] => string(1)“ 1”} [2] => array(7){[“ id”] => string(2 )“ 68” [“ restaurant_id”] =>字符串(1)“ 5” [“类别”] =>字符串(12)“茶点” [“ sub_category”] =>字符串(8)“鸡尾酒” [“价格” ] =>字符串(3)“ 999” [“描述”] =>字符串(0)“” [“可自定义”] =>字符串(1)“ 1”} [3] =>数组(7){[“ id“] => string(2)” 66“ [” restaurant_id“] => string(1)” 5“ [” category“] => string(13)” Rice biriyani“ [” sub_category“] => string( 17)“ Dal makni risotto” [“ price”] => string(3)“ 499” [“ de scription“] =>字符串(18)” Dal makhni烩饭“ [”“可定制”] =>字符串(1)“ 1”}}

3 个答案:

答案 0 :(得分:3)

如果要使用与JavaScript兼容的数组,则索引必须从0开始。最简单的方法是使用array_values()

$final['MenuList'] = array_values($menuHead);

答案 1 :(得分:2)

问题在于,当您添加data项时,您需要添加它们而没有特定的键,就像使用$i进行添加一样,

  $menuHead[$key]['data'][$i]['itemName']=$value['sub_category'];

这将阻止它们成为您希望的常规数组。对于json_encode(),数组必须从0开始并且必须是连续的,以使其成为数组。

而是一次性创建它们,然后使用[] ...将它们添加到现有数据的末尾。

  $menuHead[$key]['data'][] = ['itemName' =>$value['sub_category'],
                  'price'=> $value['price'],
                  'description'=>$value['description'],
                  'itemId'=>$value['id'],
                  'customizable'=>$value['customizable']];

这需要使用每组相似的代码来完成,其中包括整个数组本身,可以使用

$final['MenuList'] = array_values($menuHead);

要尝试修复已拥有的数据,这意味着除了添加以下代码外没有其他更改...

foreach ( $menuHead as $menu )  {
    $menu['data'] = array_values($menu['data']);
}
$final['MenuList'] = array_values($menuHead);

答案 2 :(得分:1)

使用array_values();

我修复了您的代码,它应该可以工作

    $menuHead=array(); 
    $i=0; 
    foreach($res as $key => $value){ 
        $i=$key+1;
        //$menuHead[$i]['menuHead']=$value['category'];
        if(isset($menuHead[$key]['menuHead'])){
            if($menuHead[$key]['menuHead']==$value['category']){
              $menuHead[$key]['data'][$i]['itemName']=$value['sub_category'];
              $menuHead[$key]['data'][$i]['price']=$value['price'];
              $menuHead[$key]['data'][$i]['description']=$value['description'];
              $menuHead[$key]['data'][$i]['itemId']=$value['id'];
              $menuHead[$key]['data'][$i]['customizable']=$value['customizable'];
            }else{
              $menuHead[$i]['menuHead']=$value['category'];
              $menuHead[$i]['data'][$i]['itemName']=$value['sub_category'];  
              $menuHead[$i]['data'][$i]['price']=$value['price'];
              $menuHead[$i]['data'][$i]['description']=$value['description'];
              $menuHead[$i]['data'][$i]['itemId']=$value['id'];
              $menuHead[$i]['data'][$i]['customizable']=$value['customizable'];
            }
        }else{
          $menuHead[$i]['menuHead']=$value['category'];
          $menuHead[$i]['data'][$i]['itemName']=$value['sub_category'];  
          $menuHead[$i]['data'][$i]['price']=$value['price'];
          $menuHead[$i]['data'][$i]['description']=$value['description'];
          $menuHead[$i]['data'][$i]['itemId']=$value['id'];
          $menuHead[$i]['data'][$i]['customizable']=$value['customizable'];
        }    
    }
// i'am use array_values()
    $final['MenuList']= array_values($menuHead);
    echo json_encode($final);