xml数据处理
在线测试预览地址:https://phpedia.net/#50jih7cw
- simplexml_load_string($xml)
- simplexml_load_file(‘file.xml’)
<?php
$string = <<<XML
<?xml version='1.0'?>
<string>
<title>Forty What?</title>
<from>Joe</from>
<to>Jane</to>
<body>
I know that's the answer -- but what's the question?
</body>
</string>
XML;
$str2 = <<<XML
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">
{ "status": 1, "msg":"请求成功","data":{"Xrows":["我的订单","添加订单","账号运维","常委销售查询","精细化运营管理","小组目标及达成率","查看我的客户","SKINCASE","精细化运营查询","个人门户"],"Yrowsci":["9102","6152","4342","3777","3401","2076","821","611","536","480"],"Yrowsren":["688","714","617","191","590","120","293","267","135","160"]}}
</string>
XML;
$xml = simplexml_load_string($string);
$xml2 = simplexml_load_string($str2);
$arr = json_decode(json_encode($xml2),true);
echo $arr[0];
print_r(json_encode($xml));
json_decode ( string $json [, bool $assoc = false [, int $depth = 512 [, int $options = 0 ]]] ) : mixed
assoc
当该参数为 TRUE 时,将返回 array 而非 object 。
function object2array(obj)
{
return json_decode(json_encode(), true);
}
什么是反射
看到一个概念,顺手记录下来
反射通常被定义为一个程序在执行的时候自我检查和修改自身的逻辑的能力。用较少的专业术语来说,反射就是让一个对象告诉你它自身的属性与方法,并改变哪些成员(即使是私有的)
本文介绍了使用PHP处理XML数据的方法,包括通过simplexml_load_string和simplexml_load_file加载XML字符串和文件。同时,展示了如何将SimpleXMLElement对象转换为数组和JSON格式,以及解释了反射的概念,即程序在运行时自我检查和修改自身逻辑的能力。
2636





