-1

I am working on the array to JSON object code and want the output using json_encode method.

here is the code

foreach($servicecategory as $servicecategory1) {
    $getcat = $objcat->Get_Category($servicecategory1);
    $employee = array(
            'category_id'=>mysql_real_escape_string($getcat['id']),
            'category_name'=>mysql_real_escape_string($getcat['catname']),
            'category_image_url'=>"",   
            'subcategory'=>array()
    );

    $getsubcat = $objcat->getCategoriesAdminNew($servicecategory1);

    foreach($getsubcat as $getsubcat1) {
        $getcat = $objcat->Get_Category($getsubcat1['id']); 
        $employee['subcategory'][]= array(
                'category_id'=>mysql_real_escape_string($getcat['id']),                                     
                'category_name'=>mysql_real_escape_string($getcat['catname']),
                'vendor_products_details' =>array()
        );

        $getproduct = $objuser->VendorProductDetailsNew2($userinfo['profile_id'],$getcat['id']);
        foreach($getproduct as $getproducts){
            $getcat = $objcat->Get_Category($getproducts['subcat_id']);
            $employee['vendor_products_details'][] = array(
                'product_id' => $getproducts['id'],
                'product_name' => $getproducts['pname'],
                'Price' => $getproducts['price'],
            );
        }
    }

    $data[] = $employee;
}

if($userinfo!='') {
    $infodatas=array("status"=>"success","vendor_detail"=> array($array1, $data));

    $ress=json_encode($infodatas);
    echo  $ress ;
}

it displays the output like below, which is not the proper format for json out put.

  {
    "category_id": "1",
    "category_name": "Dry Cleaning",
    "category_image_url":"",
    "subcategory": [
      {
        "sub_category_id": "4",
        "sub_category_name": "Men",
        "vendor_products_details": [

        ]
      },
      {
        "sub_category_id": "5",
        "sub_category_name": "Women",
        "vendor_products_details": [

        ]
      }

    ],
    "vendor_products_details": [
      {
        "product_id": "19",
        "product_name": "T Shirt",
        "Price": "20"
      },
      {
        "product_id": "20",
        "product_name": "Top",
        "Price": "15"
      }

    ]
  },

But I want the output in below format using multiple for each loop.

  {
    "category_id": "1",
    "category_name": "Dry Cleaning",
    "category_image_url": "",
    "subcategory": [
      {
        "sub_category_id": "4",
        "sub_category_name": "Men",
        "vendor_products_details": [
          {
        "product_id": "19",
        "product_name": "T Shirt",
        "Price": "20"
      },
      {
        "product_id": "20",
        "product_name": "Top",
        "Price": "15"
      }
        ]
      },
      {
        "sub_category_id": "5",
        "sub_category_name": "Men",
        "vendor_products_details": "Women",
        "vendor_products_details": [
          {
        "product_id": "18",
        "product_name": "T shirt",
        "Price": "15"
      },
      {
        "product_id": "9",
        "product_name": "Bedsheet",
        "Price": "15"
      }
        ]
      }


    ]

  },

same for each main category

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Sheenu
  • 31
  • 9
  • Does this answer your question? [Why shouldn't I use mysql\_\* functions in PHP?](https://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – Dharman Nov 03 '19 at 18:56
  • **Warning:** `mysql_*` extension is deprecated as of PHP 5.5.0, and has been removed as of PHP 7.0.0. Instead, either the [mysqli](https://www.php.net/manual/en/book.mysqli.php) or [PDO_MySQL](https://www.php.net/manual/en/book.pdo.php) extension should be used. See also the [MySQL API Overview](https://www.php.net/manual/en/mysqlinfo.api.choosing.php) for further help while choosing a MySQL API. – Dharman Nov 03 '19 at 18:57
  • If you are still using PHP 5 I strongly recommend to upgrade as soon as possible. This version is no longer supported. [Let Rasmus Lerdorf explain it to you](https://youtu.be/wCZ5TJCBWMg?t=2434) – Dharman Nov 03 '19 at 18:57

1 Answers1

0

Your current foreachloop states that in the get products section it should be added to $employee['vendor_products_details'][], but your explanation states that you want the products to be under your subcategory. This can be achieved by 2 small changes:

foreach($getsubcat as $getsubcat1) {
    $getcat = $objcat->Get_Category($getsubcat1['id']);
    $employee['subcategory'][$getsubcat1['id']]= array(
        'category_id'=>mysql_real_escape_string($getcat['id']),
        'category_name'=>mysql_real_escape_string($getcat['catname']),
        'vendor_products_details' =>array()
    );

    $getproduct = $objuser->VendorProductDetailsNew2($userinfo['profile_id'],$getcat['id']);
    foreach($getproduct as $getproducts){
        $getcat = $objcat->Get_Category($getproducts['subcat_id']);
        $employee['subcategory'][$getsubcat1['id']]['vendor_products_details'][] = array(
            'product_id' => $getproducts['id'],
            'product_name' => $getproducts['pname'],
            'Price' => $getproducts['price'],
        );
    }
}

What is changed: In your foreachloop with $getsubcat you need to changed the array to:

$employee['subcategory'][$getsubcat1['id']]= array(............);

In your foreachloop with $getproduct you need to change the array to:

$employee['subcategory'][$getsubcat1['id']]['vendor_products_details'][] = array(............);

Doing it this way you vendor product details will be nested in your subcategory with the id used in that specific loop.

Fabian Bron
  • 106
  • 5
  • Thanks, Fabian for your help, the above code print the below output { "category_id": "1", "category_name": "Dry Cleaning", "category_image_url": "", "subcategory": { "4": { "category_id": "4", "category_name": "Men", "vendor_products_details": [ { "product_id": "19", "product_name": "T Shirt", "Price": "20" } ] }, is it possible to remove "4": from subcategory section ,and remain will same. – Sheenu Nov 01 '19 at 01:27
  • I personally would leave the ID there so you can re-use it other function calls. However it sure is possible but the 4 will need a replacement like name or so. If you do not indicate a key to a sub category your code will not be able to see which information is linked to eachother. You will have all category id's, names and vendor details in the same array. If you do wish to remove it you can just check up where I added "$getsubcat1['id']" in the what is changed section and remove that. If you want the subcategory name you simply edit both $getsubcat1['id'] into $getsubcat1[' ... '] – Fabian Bron Nov 01 '19 at 09:18