目录
PHP json_encode() 函数介绍
首页 后端开发 php教程 php json_encode

php json_encode

Aug 08, 2016 am 09:27 AM
array encode json nbsp quot

PHP json_encode() 函数介绍


在 php 中使用 json_encode() 内置函数(php > 5.2)可以使用得 php 中数据可以与其它语言很好的传递并且使用它。

这个函数的功能是将数值转换成json数据存储格式。

01<tdcourier new border-width:0px border-left-style:solid border-left-color:rgb outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:auto line-height:1.1em min-height:inherit background:none><codecourier new min-height:inherit background:none><?php

02<tdcourier new border-width:0px border-left-style:solid border-left-color:rgb outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:auto line-height:1.1em min-height:inherit background:none><codecourier new min-height:inherit color:rgb background:none>$arr <codecourier new min-height:inherit background:none>= <codecourier new font-weight:bold min-height:inherit color:rgb background:none>array</codecourier></codecourier></codecourier></tdcourier></codecourier></tdcourier></tr></tbody></table> <p></p> <table><tbody><tr><tdcourier new border:0px outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:3em line-height:1.1em min-height:inherit color:rgb background:none><codecourier new min-height:inherit display:block background:none>03<tdcourier new border-width:0px border-left-style:solid border-left-color:rgb outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:auto line-height:1.1em min-height:inherit background:none><codecourier new min-height:inherit background:none>       <codecourier new min-height:inherit background:none>(</codecourier></codecourier></tdcourier></codecourier></tdcourier></tr></tbody></table> <p></p> <table><tbody><tr><tdcourier new border:0px outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:3em line-height:1.1em min-height:inherit color:rgb background:none><codecourier new min-height:inherit display:block background:none>04<tdcourier new border-width:0px border-left-style:solid border-left-color:rgb outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:auto line-height:1.1em min-height:inherit background:none><codecourier new min-height:inherit background:none>          <codecourier new min-height:inherit color:blue background:none>'Name'<codecourier new min-height:inherit background:none>=><codecourier new min-height:inherit color:blue background:none>'你妹'<codecourier new min-height:inherit background:none>,</codecourier></codecourier></codecourier></codecourier></codecourier></tdcourier></codecourier></tdcourier></tr></tbody></table> <p></p> <table><tbody><tr><tdcourier new border:0px outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:3em line-height:1.1em min-height:inherit color:rgb background:none><codecourier new min-height:inherit display:block background:none>05<tdcourier new border-width:0px border-left-style:solid border-left-color:rgb outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:auto line-height:1.1em min-height:inherit background:none><codecourier new min-height:inherit background:none>          <codecourier new min-height:inherit color:blue background:none>'Age'<codecourier new min-height:inherit background:none>=>22</codecourier></codecourier></codecourier></tdcourier></codecourier></tdcourier></tr></tbody></table> <p></p> <table><tbody><tr><tdcourier new border:0px outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:3em line-height:1.1em min-height:inherit color:rgb background:none><codecourier new min-height:inherit display:block background:none>06<tdcourier new border-width:0px border-left-style:solid border-left-color:rgb outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:auto line-height:1.1em min-height:inherit background:none><codecourier new min-height:inherit background:none>       <codecourier new min-height:inherit background:none>);</codecourier></codecourier></tdcourier></codecourier></tdcourier></tr></tbody></table> <p></p> <table><tbody><tr><tdcourier new border:0px outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:3em line-height:1.1em min-height:inherit color:rgb background:none><codecourier new min-height:inherit display:block background:none>07<tdcourier new border-width:0px border-left-style:solid border-left-color:rgb outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:auto line-height:1.1em min-height:inherit background:none> </tdcourier></codecourier></tdcourier></tr></tbody></table> <p></p> <table><tbody><tr><tdcourier new border:0px outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:3em line-height:1.1em min-height:inherit color:rgb background:none><codecourier new min-height:inherit display:block background:none>08<tdcourier new border-width:0px border-left-style:solid border-left-color:rgb outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:auto line-height:1.1em min-height:inherit background:none><codecourier new min-height:inherit color:rgb background:none>$jsonencode <codecourier new min-height:inherit background:none>= json_encode(<codecourier new min-height:inherit color:rgb background:none>$arr<codecourier new min-height:inherit background:none>);</codecourier></codecourier></codecourier></codecourier></tdcourier></codecourier></tdcourier></tr></tbody></table> <p></p> <table><tbody><tr><tdcourier new border:0px outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:3em line-height:1.1em min-height:inherit color:rgb background:none><codecourier new min-height:inherit display:block background:none>09<tdcourier new border-width:0px border-left-style:solid border-left-color:rgb outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:auto line-height:1.1em min-height:inherit background:none><codecourier new min-height:inherit color:rgb background:none>echo <codecourier new min-height:inherit color:rgb background:none>$jsonencode<codecourier new min-height:inherit background:none>;</codecourier></codecourier></codecourier></tdcourier></codecourier></tdcourier></tr></tbody></table> <p></p> <table><tbody><tr><tdcourier new border:0px outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:3em line-height:1.1em min-height:inherit color:rgb background:none><codecourier new min-height:inherit display:block background:none>10<tdcourier new border-width:0px border-left-style:solid border-left-color:rgb outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:auto line-height:1.1em min-height:inherit background:none><codecourier new min-height:inherit background:none>?></codecourier></tdcourier></codecourier></tdcourier></tr></tbody></table> <p>程序运行结果如下:</p> <divcourier new min-height:inherit><p></p> <p></p> <table><tbody><tr><tdcourier new border:0px outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:3em line-height:1.1em min-height:inherit color:rgb background:none><codecourier new min-height:inherit display:block background:none>1<tdcourier new border-width:0px border-left-style:solid border-left-color:rgb outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:auto line-height:1.1em min-height:inherit background:none><codecourier new min-height:inherit background:none>{<codecourier new min-height:inherit color:blue background:none>"Name"<codecourier new min-height:inherit background:none>:null,<codecourier new min-height:inherit color:blue background:none>"Age"<codecourier new min-height:inherit background:none>:20}</codecourier></codecourier></codecourier></codecourier></codecourier></tdcourier></codecourier></tdcourier></tr></tbody></table> <p>json_encode 函数中中文被编码成 null 了,Google 了一下,很简单,为了与前端紧密结合,Json 只支持 utf-8 编码,我认为是前端的 Javascript 也是 utf-8 的原因。</p> <divcourier new min-height:inherit><p></p> <p></p> <table><tbody><tr><tdcourier new border:0px outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:3em line-height:1.1em min-height:inherit color:rgb background:none><codecourier new min-height:inherit display:block background:none>1<tdcourier new border-width:0px border-left-style:solid border-left-color:rgb outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:auto line-height:1.1em min-height:inherit background:none><codecourier new min-height:inherit background:none><?php

2$array array

3    (

4        'name'=>iconv('GB18030','utf-8','你妹'),

5        'sex'=>'men'

6    );

7 

8echo json_encode($array);

9?>

这个程序的运行结果为:

1{"title":"\u6d63\u72b2\ue785","body":"men"}

   iconv 转换格式时用GBK,gb2312 报错问题,是因为GB2312只收录6763个汉字,出现未收录的汉字时,iconv报错。 GBK向下完全兼容GB2312-80,支持更多的字,但仍然会出现错误,解决办法是用GB18030,GB18030基本兼容GBK,又扩充了百余字体。

数组中所有中文在json_encode之后都不见了或者出现\u6d63等。

解决方法是用urlencode()函数处理以下,在json_encode之前,把所有数组内所有内容都用urlencode()处理一下,然用json_encode()转换成json字符串,最后再用urldecode()将编码过的中文转回来。

01<tdcourier new border-width:0px border-left-style:solid border-left-color:rgb outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:auto line-height:1.1em min-height:inherit background:none><codecourier new min-height:inherit background:none><?php

02/**************************************************************

03 *

04 *  使用特定function对数组中所有元素做处理

05 *  @param  string  &$array     要处理的字符串

06 *  @param  string  $function   要执行的函数

07 *  @return boolean $apply_to_keys_also     是否也应用到key上

08 *  @access public

09 *

10 *************************************************************/

11function arrayRecursive(&$array$function$apply_to_keys_also = false)

12{

13    static $recursive_counter = 0;

14    if (++$recursive_counter > 1000) {

15        die('possible deep recursion attack');

16    }

17    foreach ($array as $key => $value) {

18        if (is_array($value)) {

19            arrayRecursive($array[$key], $function$apply_to_keys_also);

20    &nbs
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

<🎜>:泡泡胶模拟器无穷大 - 如何获取和使用皇家钥匙
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系统,解释
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆树的耳语 - 如何解锁抓钩
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

热门话题

Java教程
1670
14
CakePHP 教程
1428
52
Laravel 教程
1329
25
PHP教程
1274
29
C# 教程
1256
24
10种在 Windows 11 上调整亮度的方法 10种在 Windows 11 上调整亮度的方法 Dec 18, 2023 pm 02:21 PM

屏幕亮度是使用现代计算设备不可或缺的一部分,尤其是当您长时间注视屏幕时。它可以帮助您减轻眼睛疲劳,提高易读性,并轻松有效地查看内容。但是,根据您的设置,有时很难管理亮度,尤其是在具有新UI更改的Windows11上。如果您在调整亮度时遇到问题,以下是在Windows11上管理亮度的所有方法。如何在Windows11上更改亮度[10种方式解释]单显示器用户可以使用以下方法在Windows11上调整亮度。这包括使用单个显示器的台式机系统以及笔记本电脑。让我们开始吧。方法1:使用操作中心操作中心是访问

如何在Safari中关闭iPhone的隐私浏览身份验证? 如何在Safari中关闭iPhone的隐私浏览身份验证? Nov 29, 2023 pm 11:21 PM

在iOS17中,Apple为其移动操作系统引入了几项新的隐私和安全功能,其中之一是能够要求对Safari中的隐私浏览选项卡进行二次身份验证。以下是它的工作原理以及如何将其关闭。在运行iOS17或iPadOS17的iPhone或iPad上,如果您在Safari浏览器中打开了任何“无痕浏览”标签页,然后退出会话或App,Apple的浏览器现在需要面容ID/触控ID认证或密码才能再次访问它们。换句话说,如果有人在解锁您的iPhone或iPad时拿到了它,他们仍然无法在不知道您的密码的情况下查看您的隐私

golang WebSocket与JSON的结合:实现数据传输和解析 golang WebSocket与JSON的结合:实现数据传输和解析 Dec 17, 2023 pm 03:06 PM

golangWebSocket与JSON的结合:实现数据传输和解析在现代的Web开发中,实时数据传输变得越来越重要。WebSocket是一种用于实现双向通信的协议,与传统的HTTP请求-响应模型不同,WebSocket允许服务器向客户端主动推送数据。而JSON(JavaScriptObjectNotation)是一种用于数据交换的轻量级格式,它简洁易读

PHP 数组转 JSON 的性能优化技巧 PHP 数组转 JSON 的性能优化技巧 May 04, 2024 pm 06:15 PM

PHP数组转JSON的性能优化方法包括:使用JSON扩展和json_encode()函数;添加JSON_UNESCAPED_UNICODE选项以避免字符转义;使用缓冲区提高循环编码性能;缓存JSON编码结果;考虑使用第三方JSON编码库。

MySQL5.7和MySQL8.0的区别是什么? MySQL5.7和MySQL8.0的区别是什么? Feb 19, 2024 am 11:21 AM

MySQL5.7和MySQL8.0是两个不同的MySQL数据库版本,它们之间有以下一些主要区别:性能改进:MySQL8.0相对于MySQL5.7有一些性能改进。其中包括更好的查询优化器、更高效的查询执行计划生成、更好的索引算法和并行查询等。这些改进可以提高查询性能和整体系统性能。JSON支持:MySQL8.0引入了对JSON数据类型的原生支持,包括JSON数据的存储、查询和索引。这使得在MySQL中处理和操作JSON数据变得更加方便和高效。事务特性:MySQL8.0引入了一些新的事务特性,如原子

使用golang中的json.MarshalIndent函数将结构体转换为格式化的JSON字符串 使用golang中的json.MarshalIndent函数将结构体转换为格式化的JSON字符串 Nov 18, 2023 pm 01:59 PM

使用golang中的json.MarshalIndent函数将结构体转换为格式化的JSON字符串在使用Golang编写程序时,我们经常需要将结构体转换为JSON字符串,在这个过程中,json.MarshalIndent函数可以帮助我们实现格式化的输出。下面我们将详细介绍如何使用这个函数,并提供具体的代码示例。首先,让我们创建一个包含一些数据的结构体。以下是示

使用PHP的json_encode()函数将数组或对象转换为JSON字符串并格式化输出 使用PHP的json_encode()函数将数组或对象转换为JSON字符串并格式化输出 Nov 03, 2023 pm 03:44 PM

使用PHP的json_encode()函数将数组或对象转换为JSON字符串并格式化输出,可以让数据在不同的平台和语言之间进行传递和交换变得更加容易。本文将介绍json_encode()函数的基本用法,以及如何将JSON字符串格式化输出。一、json_encode()函数的基本用法json_encode()函数的基本语法如下:stringjson_encod

Jackson库中注解如何控制JSON序列化和反序列化? Jackson库中注解如何控制JSON序列化和反序列化? May 06, 2024 pm 10:09 PM

Jackson库中的注解可控制JSON序列化和反序列化:序列化:@JsonIgnore:忽略属性@JsonProperty:指定名称@JsonGetter:使用获取方法@JsonSetter:使用设置方法反序列化:@JsonIgnoreProperties:忽略属性@JsonProperty:指定名称@JsonCreator:使用构造函数@JsonDeserialize:自定义逻辑

See all articles