zoukankan      html  css  js  c++  java
  • php json josn_decode()返回的是对像,如何把对像转成数组

    php json josn_decode()返回的是对像,如何把对像转成数组

    a.php传值页面,使用 json_encode($array)对数组进行加密码.

    b.php页面在接收a.php传过来的页面的值使用的是 json_decode($array),发现解密出来的数据是对象形式的:

    array(2) {
    
            [0]=>
    
            object(stdClass)#2 (4) {
    
                 ["id"]=> string(1)"1"
    
                 ["name"]=> string(9)"张雪梅"
    
                 ["age"]=> string(2)"27"
    
            object(stdClass)#3 (4) {
    
                 ["subject"]=>string(24) "计算机科学与技术"
    
            }
    
            [1]=>
    
                ["id"]=> string(1)"2"
    
                ["name"]=> string(9)"张沛霖"
    
                ["age"]=> string(2)"21"
    
               ["subject"]=> string(12) "软件工程"
    
            }
    
        }

    那么如何将数据,解密码成对象呢?

    json_decode ( string$json [, bool$assoc ] )

    说明:接受一个 JSON 格式的字符串并且把它转换为 PHP 变量。

        json_decode 可接收两个参数:

        json:待解码的jsonstring 格式的字符串。

       assoc:当该参数为 TRUE 时,将返回 array 而非 object 。

        $students = json_decode($json,true);

      这时打印一下 $students :

        var_dump($students);

    输出:

       

     array(2) {
    
            [0]=>
    
            array(4) {
    
                ["id"]=> string(1)"1"
    
                ["name"]=> string(9)"张雪梅"
    
                ["age"]=> string(2)"27"
    
                ["subject"]=>string(24) "计算机科学与技术"
    
            }
    
            [1]=>
    
            array(4) {
    
               ["id"]=> string(1)"2"
    
               ["name"]=> string(9)"张沛霖"
    
               ["age"]=> string(2)"21"
    
               ["subject"]=>string(12) "软件工程"
    
            }
    
        }

    这时,$students 就是个数组了,可以直接用

  • 相关阅读:
    oracle a:=100 和 b=:c 区别
    Oracle为表或字段添加备注
    oracle删除表字段和oracle表增加字段
    oracle数据库的一个表中,怎么设置字段的默认值
    VS2015密钥
    C# 调用WebApi
    OCX ClassId查看
    C++ 调用类的函数
    如何做一个标记为安全的ACTIVEX控件
    Java内存通道
  • 原文地址:https://www.cnblogs.com/achengmu/p/3387822.html
Copyright © 2011-2022 走看看