Coocoo4Cocoa
Coocoo4Cocoa

Reputation: 50816

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?

Upvotes: 0

Views: 2649

Answers (7)

kiran
kiran

Reputation: 11

$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"}

Upvotes: 1

Seb
Seb

Reputation: 25147

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.

Upvotes: 0

artlung
artlung

Reputation: 34013

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"]}]}

Upvotes: 0

lpfavreau
lpfavreau

Reputation: 13211

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.

Upvotes: 1

Stefan Gehrig
Stefan Gehrig

Reputation: 83622

$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.

Upvotes: 0

Paolo Bergantino
Paolo Bergantino

Reputation: 488384

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>

Upvotes: 1

Michał Niedźwiedzki
Michał Niedźwiedzki

Reputation: 12939

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.

Upvotes: 0

Related Questions