• php stdClass Object 问题


    Array ( [0] => stdClass Object ( [term_id] => 3 [name] => apache [slug] => apache [term_group] => 0 [term_taxonomy_id] => 3 [taxonomy] => category [description] => [parent] => 0 [count] => 1 [cat_ID] => 3 [category_count] => 1 [category_description] => [cat_name] => apache [category_nicename] => apache [category_parent] => 0 ) [1] => stdClass Object ( [term_id] => 1 [name] => PHP [slug] => php [term_group] => 0 [term_taxonomy_id] => 1 [taxonomy] => category [description] => [parent] => 0 [count] => 1 [cat_ID] => 1 [category_count] => 1 [category_description] => [cat_name] => PHP [category_nicename] => php [category_parent] => 0 ) )
    二维数组stdClass Object是一组json对象,如果要调用里面的数据,就要这样调用,
    foreach($categories as $key => $value){
    $cat = $value->term_id;
    $name = $value->name;
    }

    或者json_decode第二个参数设为true:

    mixedjson_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 就是个数组了,可以直接用:

    for($i=0;$i<count($students);$i++){
         echo "姓名:".$students[$i]['name']."年龄:".$students[$i]['age']."专业:".$students[$i]['subject']."<br/>";
    }

    输出结果为:

        姓名:张雪梅   年龄:27   专业:计算机科学与技术
        姓名:张沛霖   年龄:21   专业:软件工程

    总结:

    在PHP代码中处理JSON 格式的字符串的两种方法:

    方法一:

    $json= '[{"id":"1","name":"u5f20u96eau6885","age":"27","subject":"u8ba1u7b97u673au79d1u5b66u4e0eu6280u672f"},{"id":"2","name":"u5f20u6c9bu9716","age":"21","subject":"u8f6fu4ef6u5de5u7a0b"}]';

    $students= json_decode($json);//得到的是 object

    foreach($studentsas $obj){

        echo "姓名:".$obj->name."&nbsp;&nbsp;&nbsp;年 龄:".$obj->age."&nbsp;&nbsp;&nbsp;专 业:".$obj->subject."<br />";
    }

    方法二:

    $json= '[{"id":"1","name":"u5f20u96eau6885","age":"27","subject":"u8ba1u7b97u673au79d1u5b66u4e0eu6280u672f"},{"id":"2","name":"u5f20u6c9bu9716","age":"21","subject":"u8f6fu4ef6u5de5u7a0b"}]';

    $students= json_decode($json, true);//得到的是 array

    for($i=0;$i<count($students);$i++){
        echo "姓名:".$students[$i]['name']."&nbsp;&nbsp;&nbsp;年 龄:".$students[$i]['age']."&nbsp;&nbsp;&nbsp;专 业:".$students[$i]['subject']."<br />";
    }

     参考:http://blog.csdn.net/wuhanzaizai/article/details/6874845

  • 相关阅读:
    【转】编写高质量代码改善C#程序的157个建议——建议27:在查询中使用Lambda表达式
    python的reduce()函数
    SpringBoot中的配置文件
    23种设计模式概况性应用场景
    设计模式---合成模式
    tmpfs(转)
    Nginx配置文件(nginx.conf)配置详解
    Java设计模式------策略模式
    ubuntu下操作端口的方法
    ubuntu下安装ssh服务器方法
  • 原文地址:https://www.cnblogs.com/youxin/p/3891119.html
Copyright © 2020-2023  润新知