連想配列をCakePHPからjQueryに渡して展開

by yumi. 0 Comments

配列 $arr をCakePHPからjQuery に渡すには
Controllerにて

$this->set('arr', $arr);

Viewにて(※ ViewはHTMLでlayoutしているとして)

$arr = json_encode($arr);
$this->addScript($html->scriptBlock('
var arr = '.$arr.';
$.each(arr,function(k,v){
  //お好きに
});

で良いらしい。json_encode()を思い出せなかったのでメモ。

あ、それか

Controllerにて

$this->set('arr', json_encode($arr));

でも。

PHPから変数をjQueryに渡すのってHTML直書きになるからごちゃごちゃしてイヤ!でも今だけ今だけ、後で清書しよ、あーとーでー、とか思っているとそのままだったりするのは私だけですか。

コメントをどうぞ

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

*

次のHTML タグと属性が使えます: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>