码迷,mamicode.com
首页 > 其他好文 > 详细

数据格式相互转换

时间:2015-11-22 20:02:57      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:


    $array_1 = array();  //一维数组
    $array_2 = array();  //二维数组
 
    $array_1[‘username‘] = "Aseoe";
    $array_1[‘website‘] = "http://www.aseoe.com/";
 
    $array_2[‘member‘][‘ancto.net‘][‘username‘] = "ancto";
    $array_2[‘member‘][‘ancto.net‘][‘website‘] = "http://www.ancto.net/";
 
    $array_2[‘member‘][‘aseoe.com‘][‘username‘] = "aseoe";
    $array_2[‘member‘][‘aseoe.com‘][‘website‘] = "http://www.aseoe.com/";
 
    //print_r($array_2);
 
    $jsonObj_1 = json_encode($array_1);  //一维数组转换成json数据格式
    //echo $jsonObj_1;
 
    $jsonObj_2 = json_encode($array_2);     //多维数组转换成json数据格式
    //echo $jsonObj_2;
 
    class aseoe{
        public $puname = "public name";
        protected $poname = "protected name";
        private $prname = "private name";
 
        public function getName(){
            return $this->name;
        }
 
    }
 
    $aseoeObj = new aseoe();
    $json_aseoe =json_encode($aseoeObj);    //对象转换成json数据格式
    //echo $json_aseoe;    //对象转换为json数据时,只转换公有变量,私有变量不转换
 
    //print_r($aseoeObj);
 
    $jsonStr = ‘{"key":"value","key1":"value2"}‘;
    $json2Array = json_decode($jsonStr,true);  //没设第2个参数时,默认是对象类型
    print_r($json2Array);
 $jsonStr=‘{"key":"value","key1":"value1"}‘;    json_decode($jsonStr);//转换之后是对象类型    json_decode($jsonStr,true);//转换之后是数组

数据格式相互转换

标签:

原文地址:http://www.cnblogs.com/yangjinfeng172/p/4986435.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!