简体   繁体   中英

How do I properly format this json object in PHP

I need to be able to create a data structure in PHP which creates for instance, an array of car vendors. Each of those array elements contains a child array which holds types of cars for that vendor.

So you'd have something like

$cars['toyota'] = array("camry", "etc");

I need to be able to create this data structure in PHP so that on the JavaScript side of things I can do something like

alert(cars.vendor[0].type[1])

How do I encode that data structure on the PHP end of things?

If you have PHP >5.2.0, try json_encode($cars) .

This will make an array in an array, but it won't give you what you have in your javascript sample:

$vendors = array(
  'toyota' => array('camry', 'etc.'),
  'honda' => array('civic', 'fit', 'etc.'),
);

You probably don't need all the embedded levels of vendors[0].types[1] to get your information organized.

This is how to set it up to do it how you want:

<script>
    var test = <?php
        print json_encode(array('vendor' => array(
            'toyota' => array('type' => array('camry','siena')),
            'mitsubishi' => array('type' => array('mirage','galant'))
        )));
    ?>;
    alert(test.vendor['toyota'].type[1]); // siena
    alert(test.vendor['mitsubishi'].type[0]); // mirage
</script>

I would recommend skipping the vendor and type part of it altogether unless you're holding other stuff in that object too, and doing something like this:

<script>
    var vendors = <?php
        print json_encode(array(
            'toyota' => array('camry','siena'),
            'mitsubishi' => array('mirage','galant')
        ));
    ?>;
    alert(vendors['toyota'][1]); // siena
    alert(vendors['mitsubishi'][0]); // mirage
</script>
$data = array();
$data['userDetails'] =array( array("username"=>$username,"password"=>$password));
$data["wsfunction"]="user_authentication";

then output of json encode function as follows

{"userDetails":[{"username":"Username","password":"Password"}],"wsfunction":"user_authentication"}

Hey, associative arrays and objects are being treat equally in Javascript. Thus, PHP structure

$cars['toyota'] = array("camry", "etc");

would be equivalent to this in JSON:

var cars = { "toyota": [ "camry", "etc" ] };

You can easily convert PHP structure to JSON one with json_encode function. See json.org for JSON format details.

$cars = array();
$cars['toyota'] = array("camry", "etc");
$json = json_encode($cars);

will give you a javascript struct of

var cars = {
    toyota: [ 'camry', 'etc' ]
};

If you want

var cars = {
    vendor: [ { type: [ 'camry', 'etc' ] } ]
}

which will allow you to call alert(cars.vendor[0].type[1]) the PHP array should look like

$cars = array(
    'vendor' => array(
        array('type' => array("camry", "etc"))
    )
);
$json = json_encode($cars);

But as having been pointed out above you should perhaps skip the vendor -part and use the apprpriate vendor-name as the key.

Here's how I would do it:

class EmptyObject {}
$cars = new EmptyObject();
$cars->vendor[] = array('type' => array('camry','sienna'));
$cars->vendor[] = array('type' => array('mirage','galant'));
$json = json_encode($cars);
print $json;
// this produces:
// {"vendor":[{"type":["camry","sienna"]},{"type":["mirage","galant"]}]}

Well, if you're using PHP 5 you can use the function json_encode , as many have already answered.

If noy, if you're using PHP 4, you'll need something extra like this .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM