0

I am currently stuck in json formatting for php. I have given my outputted json below. What I need to do is to make the format of the current json to the desired one. I am missing the arrays in the JSON format. Can anyone help me on this.

My code to print the json output is below:

$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);

Current format:

    {
  "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"
        }
      }
    }
  }
}

Desired Format:

    {
  "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"
        }
      ]
    }
  ]
}

Raw response is below:

array(4) { [0]=> array(7) { ["id"]=> string(2) "67" ["restaurant_id"]=> string(1) "5" ["category"]=> string(11) "Main Course" ["sub_category"]=> string(18) "Chicken Thai Curry" ["price"]=> string(3) "599" ["description"]=> string(0) "" ["customizable"]=> string(1) "1" } [1]=> array(7) { ["id"]=> string(2) "69" ["restaurant_id"]=> string(1) "5" ["category"]=> string(12) "Refreshments" ["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"]=> string(1) "5" ["category"]=> string(12) "Refreshments" ["sub_category"]=> string(8) "Mocktail" ["price"]=> string(3) "999" ["description"]=> string(0) "" ["customizable"]=> string(1) "1" } [3]=> array(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" ["description"]=> string(18) "Dal makhni risotto" ["customizable"]=> string(1) "1" } }

1
  • Arrays start at 0! Commented Nov 7, 2018 at 12:07

3 Answers 3

3

If you want a javascript compatible array, the index must start at 0. The easiest way to do that, is to use array_values():

$final['MenuList'] = array_values($menuHead);
Sign up to request clarification or add additional context in comments.

Comments

2

The problem is that when your adding the data items, you need to add them without specific keys, as you add them with $i as in...

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

This will stop them being a normal array as you want it to be. For json_encode() an array must start at 0 and be sequential for it to be an array.

Instead create them in one go and add them to the end of the existing data using []...

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

This needs to be done with each set of similar code, which includes the overall array itself, this can be done using

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

To try and fix the data you already have, which means no changes except adding the following code...

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

7 Comments

This solution didnt work out. Is there any other way to do it?
I've added a new way at the bottom of the answer, this should be used with your original code.
Yes I did change it in 3 places
You may need $menuHead as &$menu to allow it to update the original data, see if it helps.
foreach ( $menuHead as $menu ) { $menu['data'] = array_values($menu['data']); } $final['MenuList'] = array_values($menuHead); Added this but it didnt work out
|
1

Use array_values();

I fixed your code, it should work

    $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);

4 Comments

Your code added the array in menulist which my code was missing but the data object must also be an array. Kindly help on this
$final['MenuList']= (object) array_values($menuHead)
No it didnt work. data json object must also be an array
Try this. $final['MenuList'][] = array_values($menuHead);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.