Home php教程 PHP源码 PHP object(SimpleXMLElement) 对象转换为数组

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

Jun 06, 2016 am 09:32 AM

跳至 [1] [2] [全屏预览]
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)
Copy after login

2. [代码]示例:PHP object(SimpleXMLElement) 对象转换为数组的方式 跳至 [1] [2] [全屏预览]

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;
    }
}
Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1269
29
C# Tutorial
1248
24