zoukankan      html  css  js  c++  java
  • PHP json_encode() 函数介绍

    $data ={"itemName":"Giordana","itemId":450,"itemPrice":45,"itemCategory":"Shirit",};
    $result
    = json_decode($data,true);

    You need to do above two steps in the controller function and then if you want to show that json data in the view file You just need to pass that array $result to view.

     

    $this->load->view('view_file',$data);

    http://blog.csdn.net/gumincong/article/details/7738444

     

    PHP json_encode() 函数介绍

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

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

    01 <?php
    02 $arr=array
    03        (
    04           'Name'=>'希亚',
    05           'Age'=>20
    06        );
    07  
    08 $jsonencode= json_encode($arr);
    09 echo$jsonencode;
    10 ?>

    程序运行结果如下:

    1 {"Name":null,"Age":20}

    json_encode 函数中中文被编码成 null 了,Google 了一下,很简单,为了与前端紧密结合,Json 只支持 utf-8 编码,我认为是前端的 Javascript 也是 utf-8 的原因。

    1 <?php
    2 $array=array
    3     (
    4         'title'=>iconv('gb2312','utf-8','这里是中文标题'),
    5         'body'=>'abcd...'
    6     );
    7  
    8 echojson_encode($array);
    9 ?>

    这个程序的运行结果为:

    1 {"title":"\u8fd9\u91cc\u662f\u4e2d\u6587\u6807\u9898","body":"abcd..."}

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

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

    01 <?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 *************************************************************/
    11 functionarrayRecursive(&$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($arrayas$key=> $value) {
    18         if(is_array($value)) {
    19             arrayRecursive($array[$key],$function,$apply_to_keys_also);
    20         }else{
    21             $array[$key] =$function($value);
    22         }
    23  
    24         if($apply_to_keys_also&&is_string($key)) {
    25             $new_key=$function($key);
    26             if($new_key!=$key) {
    27                 $array[$new_key] =$array[$key];
    28                 unset($array[$key]);
    29             }
    30         }
    31     }
    32     $recursive_counter--;
    33 }
    34  
    35 /**************************************************************
    36 *
    37 *  将数组转换为JSON字符串(兼容中文)
    38 *  @param  array   $array      要转换的数组
    39 *  @return string      转换得到的json字符串
    40 *  @access public
    41 *
    42 *************************************************************/
    43 functionJSON($array) {
    44     arrayRecursive($array,'urlencode', true);
    45     $json= json_encode($array);
    46     returnurldecode($json);
    47 }
    48  
    49 $array=array
    50        (
    51           'Name'=>'希亚',
    52           'Age'=>20
    53        );
    54  
    55  
    56 echoJSON($array);
    57 ?>

    这次成功了,运行结果如下:

    1 {"Name":"希亚","Age":"20"}
  • 相关阅读:
    npm install 报错问题解决
    搭建VUE脚手架流程
    gitLad创建新仓库、上传项目、拉取项目方法
    vscode打开终端失败,重新安装其它版本也没用,想知道这是为什么
    Vue组件 父组件使用子组件中的值(子组件给父组件传值的方法)
    Element-ui 上传el-upload组件的使用方法
    Vue 计算属性
    JQ+Layui弹窗 实现支付功能
    Vue+ElementUI 验证手机号发送验证码倒计时功能,先清除定时器然后创建定时器
    生命陪伴生命,生命唤醒生命
  • 原文地址:https://www.cnblogs.com/fx2008/p/2961506.html
Copyright © 2011-2022 走看看