学习整理使用php将SimpleXMLElement 对象解析成数组格式的方法
学习整理使用php将SimpleXMLElement 对象解析成数组格式的方法
要将 SimpleXMLElement 对象解析成数组格式,您可以使用 PHP 的 json_decode 和 json_encode 函数。首先,将 SimpleXMLElement 对象转换为 JSON 字符串,然后将这个字符串解码成数组。以下是具体的步骤和代码示:
<?php// 假设 $xmlObject 是您的 SimpleXMLElement 对象$xmlObject = new SimpleXMLElement('<response><code>15</code><msg>Remote service error</msg><sub_code>4006</sub_code><sub_msg>api请求异常:店铺服务异常,原因:系统异常,请稍后重试,traceId:2107620517465959198326947ec0f3</sub_msg><request_id>16lrdaxltmo2b</request_id></response>'
);// 将 SimpleXMLElement 对象转换为 JSON 字符串
$jsonString = json_encode($xmlObject);// 将 JSON 字符串解码成数组
$arrayFormat = json_decode($jsonString, true);// 打印数组格式
print_r($arrayFormat);?>