php simplexmlelement object,PHP object(SimpleXMLElement) 对象转换为数组

更多PHP小窍门:http://www.libs.org.cn

1.[代码]示例:PHP 输出的object(SimpleXMLElement)结果

object(SimpleXMLElement)[9]

public 'result' =>

object(SimpleXMLElement)[8]

public 'err_code' => string '0' (length=1)

public 'model' => string '000000000000^000000000000' (length=26)

public 'success' => string 'true' (length=4)

public 'request_id' => string '000000000000' (length=13)

2.[代码]示例:PHP object(SimpleXMLElement) 对象转换为数组的方式

class xml{

/**

* object(SimpleXMLElement) 对象转换为数组

* convert simplexml object to array sets

* $array_tags 表示需要转为数组的 xml 标签。例:array('item', '')

* 出错返回False

*

* @param object $simplexml_obj

* @param array $array_tags

* @param int $strip_white 是否清除左右空格

* @return mixed

*/

public static function toArray($simplexml_obj, $array_tags=array(), $strip_white=1)

{

if( $simplexml_obj )

{

if( count($simplexml_obj)==0 )

return $strip_white?trim((string)$simplexml_obj):(string)$simplexml_obj;

$attr = array();

foreach ($simplexml_obj as $k=>$val) {

if( !empty($array_tags) && in_array($k, $array_tags) ) {

$attr[] = self::toArray($val, $array_tags, $strip_white);

}else{

$attr[$k] = self::toArray($val, $array_tags, $strip_white);

}

}

return $attr;

}

return false;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值