Sasi Kumar
Sasi Kumar

Reputation: 13368

How to create JSON Multidimensional Array in PHP code?

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"

        }]
    }


]
}

Upvotes: 1

Views: 7724

Answers (2)

Utsav Kundu
Utsav Kundu

Reputation: 11

$make=array();
$vtype=array();
$index = array(
   'make'=>$make,
   'vtype'=>$vtype,
);

Upvotes: 0

Aref Anafgeh
Aref Anafgeh

Reputation: 512

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

Upvotes: 2

Related Questions