I am working on the array to JSON object code and want the output using the 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(
'subcategory_id'=>mysql_real_escape_string($getcat['id']),
'subcategory_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 below, which is not the proper format for JSON output.
{
"category_id": "1",
"category_name": "Dry Cleaning",
"category_image_url":"",
"subcategory": [
{
"subcategory_id": "4",
"subcategory_name": "Men",
"vendor_products_details": [
]
},
{
"subcategory_id": "5",
"subcategory_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 the below format using multiple for each loop. for category->subcategory->product, the product will be a show based on sub-category
{
"category_id": "1",
"category_name": "Dry Cleaning",
"category_image_url": "",
"subcategory": [
{
"subcategory_id": "4",
"subcategory_name": "Men",
"vendor_products_details": [
{
"product_id": "19",
"product_name": "T Shirt",
"Price": "20"
},
{
"product_id": "20",
"product_name": "Top",
"Price": "15"
}
]
},
{
"subcategory_id": "5",
"subcategory_name": "Women",
"vendor_products_details": [
{
"product_id": "18",
"product_name": "T shirt",
"Price": "15"
},
{
"product_id": "9",
"product_name": "Bedsheet",
"Price": "15"
}
]
}
]
},
Actually the output will be based on category, subcategory based on category and products based on subcategory in json object
Top comments (1)
It seems that data you are getting is not is the cause of error. vendor_product_details are not properly linked to subcategory. Remember PHP arrays are a trick .
Here is a thought, Try dumping the array itself and see if nested keys are what you wanted. print_r() should be still available even though I would prefer you use some proper debugging tool like Symphony:Dump . It would give more detailed insight.