DRK DRK - 7 months ago 11
PHP Question

how to create php json web service for this json

How do I create PHP JSON web service for this JSON?

The inside category will have more products.

{
"category": [{
"term_id": "1",
"name": "water",
"slug": "water",
"products": [{
"product_id": "1",
"product_name": "water 1L",
"product_rating": "5"
}, {
"product_id": "2",
"product_name": "water 2L",
"product_rating": "4"
}]
}, {
"term_id": "2",
"name": "car",
"slug": "car",
"products": [{
"product_id": "3",
"product_name": "benz",
"product_rating": "5"
}, {
"product_id": "4",
"product_name": "toyota",
"product_rating": "3"
}]
}]
}

Answer
$categories=array();
$products=array();
array_push($products,array('product_id'=>'1',"product_name"=>"water_1l","product_rating"=>'5'));
array_push($products,array('product_id'=>'2',"product_name"=>"water_2l","product_rating"=>'4'));
array_push($categories,array('term_id'=>'1','name'=>'water','slug'=>'water','products'=>$products));
//just empty the array $products
unset($products);
$products=array();
array_push($products,array('product_id'=>'3',"product_name"=>"benz","product_rating"=>'5'));
array_push($products,array('product_id'=>'4',"product_name"=>"toyata","product_rating"=>'3'));
array_push($categories,array('term_id'=>'2','name'=>'car','slug'=>'car','products'=>$products));
// this will show you the output in json string
echo '<pre>';
echo json_encode(array('categories'=>$categories));