2009-07-13 12 views
0

これは見つかりましたか?これはこれを行う最善の方法ですか?JSONデータのテンプレート?

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

私は、JSONデータに投げる変数を繰り返しループのいくつかの並べ替えを使用していますよ。

私はCodeignitorとjqueryを使用しています。

おかげ

+0

例はASPにありますが、PHPを使用していますが、「最善の方法」とはどういう意味ですか? –

+0

この関数はjsonであり、jsonデータを読み込むPHPのテンプレートを設計する最良の方法です。 – matthewb

答えて

1

あなたはPHPの変数やオブジェクトにJSONを回す何かをしたいという意味ならば、私はこのコードはそれを説明すると思います:

コード:

<? 
// 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); 

出力:

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 
     ) 

) 
+0

これはPHP 5.2以降でのみ動作することに注意してください。マニュアルページhttp://us3.php.net/manual/en/ref.json.phpを参照してください。 – tj111