views:

291

answers:

1

I found this? Is this the best way to do it?

http://weblogs.asp.net/dwahlin/archive/2009/05/03/using-jquery-with-client-side-data-binding-templates.aspx

I'm looking to use some sort of repeat loop with variables that throw in JSON data.

I am using Codeignitor and jquery.

Thanks

+1  A: 

If you mean that you want something that turns JSON into a PHP variable or object, I think this code will explain it:

Code:

<?
// here is an array
$myarray = array(
    'animal' => 'dog',
    'plant' => 'tree',
    'anotherArray' => array ('some' => 'data'),
);

// print out the array to show what it looks like
print_r($myarray);

// convert the array to json
$jsonArray = json_encode($myarray);

// print out the json data
print $jsonArray.'\n';

// convert the json data back into a PHP array
$phpArray = json_decode($jsonArray);

// print out the array that went from PHP to JSON, and back to PHP again
print_r($phpArray);

Output:

Array
(
    [animal] => dog
    [plant] => tree
    [anotherArray] => Array
        (
            [some] => data
        )

)
{"animal":"dog","plant":"tree","anotherArray":{"some":"data"}}
stdClass Object
(
    [animal] => dog
    [plant] => tree
    [anotherArray] => stdClass Object
        (
            [some] => data
        )

)
Stephen J. Fuhry
Keep in mind this only works on PHP 5.2 and newer. See the man pages http://us3.php.net/manual/en/ref.json.php
tj111