在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
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; } 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++){ 输出结果为: 姓名:张雪梅 年龄:27 专业:计算机科学与技术 总结: 在PHP代码中处理JSON 格式的字符串的两种方法: 方法一: $json= '[{"id":"1","name":"\u5f20\u96ea\u6885","age":"27","subject":"\u8ba1\u7b97\u673a\u79d1\u5b66\u4e0e\u6280\u672f"},{"id":"2","name":"\u5f20\u6c9b\u9716","age":"21","subject":"\u8f6f\u4ef6\u5de5\u7a0b"}]'; $students= json_decode($json);//得到的是 object foreach($studentsas $obj){ echo "姓名:".$obj->name." 年 龄:".$obj->age." 专 业:".$obj->subject."<br />";
方法二: $json= '[{"id":"1","name":"\u5f20\u96ea\u6885","age":"27","subject":"\u8ba1\u7b97\u673a\u79d1\u5b66\u4e0e\u6280\u672f"},{"id":"2","name":"\u5f20\u6c9b\u9716","age":"21","subject":"\u8f6f\u4ef6\u5de5\u7a0b"}]'; $students= json_decode($json, true);//得到的是 array for($i=0;$i<count($students);$i++){ 参考:http://blog.csdn.net/wuhanzaizai/article/details/6874845
|
2022-08-17
2022-11-06
2022-08-17
2022-07-18
2022-07-22
请发表评论