1

I Need JSON encode format like below structure.Product,productinfo values are dynamically get from MySQL database.

{
"Product": [{
        "mainid": "1",
        "productinfo": [{
            "subid": "1222",
            "rate": "4"

        }, {
            "subid": "1222",
            "rate": "74"

        }, {
            "subid": "1222",
            "rate": "49"

        }]
    }, {
        "mainid": "2",
        "productinfo": [{
            "subid": "224",
            "rate": "86"

        }, {
            "subid": "122255",
            "rate": "55"

        }, {
            "subid": "12252",
            "rate": "54"

        }]
    }


]
}
sasikumar
  • 12,540
  • 3
  • 28
  • 48

2 Answers2

2

fist of all creat a multidimensioal array to respresent that then encode it to json format.like this:

$array = array(
 'product'=>array(
    'mainid'=>1,
     "productinfo"=>array(
       0=>array('subid'=>1222 ,'rate'=>4),
       1=>array("subid"=>"1222",
        "rate"=>"74")
        //and so on
     ) 
  ),
);

then you encode it into josn using:

json_encode($array);
Aref Anafgeh
  • 512
  • 1
  • 6
  • 20
  • yes it gives correct format.but i need from dynamic values from date base.how can i change this code for my requirement? – sasikumar Jan 01 '16 at 17:02
  • @sasikumar oooh man this can not be done via a question,you need to know database(like mysql) ,php ,PDO and...this is a mini project it self.you asked how to create json – Aref Anafgeh Jan 01 '16 at 17:14
  • yes..am accept your answer.its work static value..an android developer.i need some guidance that why am ask..don't mistake me – sasikumar Jan 01 '16 at 17:16
  • @sasikumar ok.you have a database ant it has a product table. using PDO (this is what i recommend) create a connection to database in your php script then it returns a set of products.use foreach to go throgh each of results and store each row wanted fields in a multi dimensioal array and then produce json.i hope i could have helped you to gain an overall guid.good luck – Aref Anafgeh Jan 01 '16 at 17:21
  • if u can possible means to change code value dynamically in small example. – sasikumar Jan 01 '16 at 17:21
  • just put code in foreach loop thats enough for me..change in foreach – sasikumar Jan 01 '16 at 17:23
  • ok use this link to use pdo:http://stackoverflow.com/questions/10911757/how-to-use-pdo-to-fetch-results-array-in-php – Aref Anafgeh Jan 01 '16 at 17:24
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/99474/discussion-between-sasikumar-and-aref-anafgeh). – sasikumar Jan 01 '16 at 17:26
  • thanks for your help ..i got solution refered by this link http://stackoverflow.com/questions/7821878/php-create-dynamic-array-in-a-multidimensional-array – sasikumar Jan 01 '16 at 17:32
0
$make=array();
$vtype=array();
$index = array(
   'make'=>$make,
   'vtype'=>$vtype,
);
Frits
  • 7,341
  • 10
  • 42
  • 60