views:

719

answers:

6

I need to be able to create a data structure in PHP which creates for instance, an array of car vendors. Each of those arrays, has child arrays which are 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?

+1  A: 

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.

lpfavreau
My question is how does the array have to look in PHP before json_encode spits out the format I want it in? an array of arrays is what I'm looking for.
Coocoo4Cocoa
A: 

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.

Michał Rudnicki
+1  A: 

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>
Paolo Bergantino
A: 
$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.

Stefan Gehrig
A: 

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"]}]}
artlung
A: 

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.

Seb