A frequently asked question in GoogleGroups is: how to pass a PHP array to Javascript. the answer is to convert the PHP array to JSON. There are several solutions: including third-party PHP class libraries and PHP5JSON extensions (php_json.dll or json. so ). however, the recommended method is to use javascriptHelper of CakePHP. as a pure PHP Solution
A frequently asked question in Google Groups is: how to pass a PHP array to Javascript. the answer is to convert the PHP array to JSON. There are several solutions: including third-party PHP class libraries and PHP5 JSON extensions (php_json.dll or json. so ). however, the recommended method is to use javascript Helper of CakePHP. as a pure PHP Solution
A frequently asked question in Google Groups is: how to pass a PHP array
Javascript. The answer is to convert the PHP array to JSON. There are several solutions: including third-party PHP
Class Library, PHP5 JSON extension (php_json.dll or json. so ).
However, the recommended method is to use javascript Helper of CakePHP as a pure PHP Solution.
It works on both PHP4 and PHP5.
Using it is as simple as using other Helper. Suppose there are some arrays in your controller and pass them
The view can be used.
Okay. Here are some code snippets and outputs.
Controller code (PHP ):
$ My_array = array (1, 2, 3, 4, 5 );
$ My_array2 = array ('one' => '1', 'two' => '2', '3 ');
$ This-> set (compact ('My _ array', 'My _ array2 '));
View code (PHP ):
Echo $ javascript-> Object ($ my_array );
Echo $ javascript-> Object ($ my_array2 );
The output is (Javascript ):
[1, 2, 3, 4, 5]
{"One": 1, "two": 2, "0": 3}
The output result of the more complex Cheesecake-Photoblog findAll method is as follows (PHP ):
Array
(
[0] => Array
(
[Photo] => Array
(
[Id] => 2
[Filename] => 1180944624_3dgreen.png
[Title] => 3D Green
[Created] => 13:40:00
)
)
[1] => Array
(
[Photo] => Array
(
[Id] => 1
[Filename] => 1180938295_FreshFlower.jpg
[Title] => Fresh Flower
[Created] => 11:54:00
)
)
)
JSON result:
[{"Photo": {"id": 2, "filename": "1180944624_3dgreen.png", "title": "3D Green", "created ": "13:40:00" }}, {"Photo": {"id": 1, "filename": "1180938295_FreshFlower.jpg", "title": "Fresh Flower", "created ": "11:54:00"}]