Code前端首页关于Code前端联系我们

包含php的json_encode的词条

terry 2年前 (2023-09-30) 阅读数 65 #PHP
文章标签 PHP microtime

本文目录一览:

  • 1、php 中 json_encode 只保留 value
  • 2、php的json_encode如何让中文不要编码保持原始值?
  • 3、PHP中json_encode中文乱码问题
  • 4、php中关于json_encode的问题
  • 5、如何让php jsonencode 不转义unicode
  • 6、PHP json_encode 变量如何转换成数组?

php 中 json_encode 只保留 value

循环将每个数组的值装入一个新数组,然后将新数组转换成json格式即可实现。示例如下:

?php

header("Content-type:text/html;charset=utf-8;");

$str='[{"id":"1","数据1":"11","数据2":"12"},{"id":"2","数据1":"21","数据2":"22"},{"id":"3","数据1":"31","数据2":"32"}]';

$data=json_decode($str,true);

$arr=array();

foreach ($data as $key=$item){

    $arr[]=array_values($item);

    

}

print_r(json_encode($arr));

//[["1","11","12"],["2","21","22"],["3","31","32"]]

?

php的json_encode如何让中文不要编码保持原始值?

json_encode只支持UTF8编码的字符,保证在使用JSON处理的时候字符是以UTF8编码的。具体我们可以把数据库编码和页面编码都改为UTF8。当然喜欢用gbk编码的话,可以在进行JSON处理前,把字符转为UTF8形式。济南诺洋网络为您解答,希望可以帮到您。

PHP中json_encode中文乱码问题

php 中使用 json_encode() 内置函数(php 5.2)可以使用得 php 中数据可以与其它语言很好的传递并且使用它。这个函数的功能是将数值转换成json数据存储格式

$arr = array ('a'=1,'b'=2,'c'=3,'d'=4,'e'=5);

echo json_encode($arr);

//结果

//{"a":1,"b":2,"c":3,"d":4,"e":5}

/*

下面看一款json_encode中文乱码问题

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

*/

function arrayrecursive($array, $function, $apply_to_keys_also = false)

{

static $recursive_counter = 0;

if (++$recursive_counter 1000) {

die('possible deep recursion attack');

}

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

if (is_array($value)) {

arrayrecursive($array[$key], $function, $apply_to_keys_also);

} else {

$array[$key] = $function($value);

}

if ($apply_to_keys_also is_string($key)) {

$new_key = $function($key);

if ($new_key != $key) {

$array[$new_key] = $array[$key];

unset($array[$key]);

}

}

}

$recursive_counter--;

}

/**************************************************************

*

* 将数组转换为json字符串(兼容中文)

* @param array $array 要转换的数组

* @return string 转换得到的json字符串

* @access public

*

*************************************************************/

function json($array) {

arrayrecursive($array, 'urlencode', true);

$json = json_encode($array);

return urldecode($json);

}

$array = array

(

'name'='希亚',

'age'=20

);

echo json($array);

//应用实例

$servname="localhost";

$sqlservname="root";

$sqlservpws="123456";

$sqlname="lock1";

$db=mysql教程_connect($servname,$sqlservname,$sqlservpws) or die("数据库教程连接失败");

mysql_select_db($sqlname,$db);

$sql = "select * from t_operater";

$result =mysql_query($sql);

$rows = mysql_num_rows($result);

while($obj = mysql_fetch_object($result))

{

$arr[] = $obj;

}

echo '({"total":"'.$rows.'","results":'.json_encode($arr).'})';

php中关于json_encode的问题

是的,["Apple","Banana","Pear"]是json的数组格式

需要在php上改动:

var_dump(json_encode(array("Apple", "Banana", "Pear"), JSON_FORCE_OBJECT));

所选答案错误,有眼无珠

如何让php jsonencode 不转义unicode

方法有两种

1:php版本大于等于5.4,在使用json_encode的时候加入第二个参数JSON_UNESCAPED_UNICODE:如

//这样使用中文可不会被转义

$jsonStr = json_encode($data,JSON_UNESCAPED_UNICODE);

2:如果php版本小于5.4,可以使用这种方法,对unicode码再进行解码,解码函数如下:

/**

    * 因php版本小于 5.4

    * decodeUnicode 对中文编码unicode进行反转义为汉字

    * @param  $str json字符串

    * @return 转义后的json字符串

    */

   function decodeUnicode($str)

   {

       return preg_replace_callback('/\\\\u([0-9a-f]{4})/i',

           create_function(

               '$matches',

               'return mb_convert_encoding(pack("H*", $matches[1]), "UTF-8", "UCS-2BE");'

           ),

           $str);

   }

   

   //使用方法

   $jsonStr = decodeUnicode(json_encode($data));

   //或者单独封装一个函数

   function _json_encode($data=array()){

       return decodeUnicode(json_encode($data));

   }

PHP json_encode 变量如何转换成数组?

1json_decode(json数据, true); //得到一个数组

json_encode()函数和json_decode()是一对函数

作用刚好相反

一个是将数组转换成json数据

一个是将json数据转换成数组

版权声明

本文仅代表作者观点,不代表Code前端网立场。
本文系作者Code前端网发表,如需转载,请注明页面地址。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门