Home php教程 php手册 在CakePHP中利用Javascript Helper把PHP数组转换为JSON

在CakePHP中利用Javascript Helper把PHP数组转换为JSON

Jun 06, 2016 pm 07:51 PM
cakephp helper javascript p use

在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"}}] 

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1656
14
PHP Tutorial
1257
29
C# Tutorial
1229
24
CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

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

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

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

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

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 implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

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

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

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

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

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

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

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

CakePHP Logging CakePHP Logging Sep 10, 2024 pm 05:26 PM

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

See all articles