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"

        }]
    }


]
}
2
  • Do you know how to create multi dimensional array in PHP? If so, it's simple to convert that array into json array using function json_encode function in PHP Commented Jan 1, 2016 at 7:48
  • did my answer helped you? Commented Jan 1, 2016 at 9:43

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

9 Comments

yes it gives correct format.but i need from dynamic values from date base.how can i change this code for my requirement?
@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
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 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
if u can possible means to change code value dynamically in small example.
|
0
$make=array();
$vtype=array();
$index = array(
   'make'=>$make,
   'vtype'=>$vtype,
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.