在CakePHP中利用Javascript Helper把PHP数组转换为JSON
在Google Groups中一个经常问到的问题是:如何把一个PHP数组传递给 Javascript.答案是把PHP数组转换为JSON,有几个解决办法:包括第三方PHP 类库,PHP5 JSON扩展(php_json.dll 或则 json.so). 但是推荐的方法是使用CakePHP的javascript Helper.作为一个纯PHP的解
在Google Groups中一个经常问到的问题是:如何把一个PHP数组传递给
Javascript.答案是把PHP数组转换为JSON,有几个解决办法:包括第三方PHP
类库,PHP5 JSON扩展(php_json.dll 或则 json.so).
但是推荐的方法是使用CakePHP的javascript Helper.作为一个纯PHP的解决方法
它能够同时在PHP4和PHP5上工作
使用它像使用其他Helper一样简单,假设在你的控制器中有一些数组,把它们传递给
视图就可使用了.
好了,下面是一些代码片段和输出
控制器代码(PHP):
$my_array = array(1,2,3,4,5);
$my_array2= array('one'=>'1','two'=>'2','3');
$this->set(compact('my_array','my_array2'));
视图代码(PHP):
echo $javascript->Object($my_array);
echo $javascript->Object($my_array2);
输出为(Javascript):
[1,2,3,4,5]
{"one":1, "two":2, "0":3}
更复杂的Cheesecake-Photoblog findAll方法的输出结果如下(PHP):
Array
(
[0] => Array
(
[Photo] => Array
(
[id] => 2
[filename] => 1180944624_3dgreen.png
[title] => 3D Green
[created] => 2007-06-04 13:40:00
)
)
[1] => Array
(
[Photo] => Array
(
[id] => 1
[filename] => 1180938295_FreshFlower.jpg
[title] => Fresh Flower
[created] => 2007-06-04 11:54:00
)
)
)
JSON结果:
[{"Photo":{"id":2, "filename":"1180944624_3dgreen.png", "title":"3D Green", "created":"2007-06-04 13:40:00"}}, {"Photo":{"id":1, "filename":"1180938295_FreshFlower.jpg", "title":"Fresh Flower", "created":"2007-06-04 11:54:00"}}]

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Validator can be created by adding the following two lines in the controller.

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

In this chapter, we are going to learn the following topics related to routing ?

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide
