zoukankan      html  css  js  c++  java
  • PHP基础回顾之PHP JSON(六)

    JSON 函数

    PHP 数组转换为 JSON 格式数据:

    <?php
       $arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
       echo json_encode($arr);
    ?>
    

    PHP 对象转换为 JSON 格式数据:

    <?php
       class Emp {
           public $name = "";
           public $hobbies  = "";
           public $birthdate = "";
       }
       $e = new Emp();
       $e->name = "sachin";
       $e->hobbies  = "sports";
       $e->birthdate = date('m/d/Y h:i:s a', "8/5/1974 12:20:03 p");
       $e->birthdate = date('m/d/Y h:i:s a', strtotime("8/5/1974   12:20:03"));
       echo json_encode($e);
    ?>
    //代码执行结果为:
    //{"name":"sachin","hobbies":"sports","birthdate":"08/05/1974 12:20:03 pm"}
    

    json_decode
    PHP json_decode() 函数用于对 JSON 格式的字符串进行解码,并转换为 PHP 变量。

    解码 JSON 数据:

    <?php
       $json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
       var_dump(json_decode($json));
       var_dump(json_decode($json, true));
    ?>
    

    以上代码执行结果为:

    object(stdClass)#1 (5) {
        ["a"] => int(1)
        ["b"] => int(2)
        ["c"] => int(3)
        ["d"] => int(4)
        ["e"] => int(5)
    }
    
    array(5) {
        ["a"] => int(1)
        ["b"] => int(2)
        ["c"] => int(3)
        ["d"] => int(4)
        ["e"] => int(5)
    }
    
  • 相关阅读:
    ArcEngine 图层渲染(转)
    新年快乐!
    js限制输入(转载)
    ArcEngine中使用上下左右键移动地图
    ArcEngine 渲染的使用
    Geometry 对象浅析
    AE数据加载
    基于arcgis server地图页面设计备忘
    AO中的一些基础类和接口
    ArcGIS Server 9.2数据加载
  • 原文地址:https://www.cnblogs.com/smileyqp/p/12675408.html
Copyright © 2011-2022 走看看