AI编程助手
AI免费问答

php解析JSON与XML数据的实现代码

  2016-07-25 08:58   1444浏览 原创
  1. $json_string='{"id":1,"name":"foo","email":"foo@jbxue.com","interest":["wordpress","php"]} ';
  2. $obj=json_decode($json_string);
  3. echo $obj->name; //prints foo
  4. echo $obj->interest[1]; //prints php
复制代码

2,PHP解析XML 数据

  1. $xml_string="

  2. test
  3. test@jbxue.com
  4. news
  5. fnews@jbxue.net
  6. ";
  7. //load the xml string using simplexml

  8. $xml = simplexml_load_string($xml_string);
  9. //loop through the each node of user

  10. foreach ($xml->user as $user)
  11. {
  12. //access attribute
  13. echo $user['id'], ' ';
  14. //subnodes are accessed by -> operator
  15. echo $user->name, ' ';
  16. echo $user->email, '
    ';
  17. }
复制代码

如今,json在数据传输上的应用越来越广了,建议大家好好掌握下。



php免费学习视频:立即学习
踏上前端学习之旅,开启通往精通之路!从前端基础到项目实战,循序渐进,一步一个脚印,迈向巅峰!

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。