php函数描述及例子

/**

* xml2array() will convert the given XML text to an array in the XML structure. 
* Link: http://www.bin-co.com/php/scripts/xml2array/ 
* Arguments : $contents - The XML text 
*                 $get_attributes - 1 or 0. If this is 1 the function will get the attributes as well as the tag values - this results in a different array structure in the return value. 
*                 $priority - Can be ‘tag‘ or ‘attribute‘. This will change the way the resulting array sturcture. For ‘tag‘, the tags are given more importance. 
* Return: The parsed XML in an array form. Use print_r() to see the resulting array structure. 
* Examples: $array =   xml2array(file_get_contents(‘feed.xml‘)); 
*               $array =   xml2array(file_get_contents(‘feed.xml‘, 1, ‘attribute‘)); 
*/ 
function xml2array($contents, $get_attributes=1, $priority = ‘tag‘) { 
     if(!$contents) return array();

if(!function_exists(‘xml_parser_create‘)) { 
        //print "‘xml_parser_create()‘ function not found!"; 
        return array(); 
     }

//Get the XML parser of PHP - PHP must have this module for the parser to work 
    $parser = xml_parser_create(‘‘); 
    xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8"); # http://minutillo.com/steve/weblog/2004/6/17/php-xml-and-character-encodings-a-tale-of-sadness-rage-and-data-loss 
    xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); 
    xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1); 
    xml_parse_into_struct($parser, trim($contents), $xml_values); 
    xml_parser_free($parser);

if(!$xml_values) return;//Hmm...

//Initializations 
    $xml_array = array(); 
    $parents = array(); 
    $opened_tags = array(); 
    $arr = array();

$current = &$xml_array; //Refference

//Go through the tags. 
    $repeated_tag_index = array();//Multiple tags with same name will be turned into an array 
    foreach($xml_values as $data) { 
         unset($attributes,$value);//Remove existing values, or there will be trouble

//This command will extract these variables into the foreach scope 
         // tag(string), type(string), level(int), attributes(array). 
        extract($data);//We could use the array by itself, but this cooler.

$result = array(); 
        $attributes_data = array(); 
          
         if(isset($value)) { 
             if($priority == ‘tag‘) $result = $value; 
             else $result[‘value‘] = $value; //Put the value in a assoc array if we are in the ‘Attribute‘ mode 
        }

//Set the attributes too. 
        if(isset($attributes) and $get_attributes) { 
             foreach($attributes as $attr => $val) { 
                 if($priority == ‘tag‘) $attributes_data[$attr] = $val; 
                 else $result[‘attr‘][$attr] = $val; //Set all the attributes in a array called ‘attr‘ 
            } 
         }

//See tag status and do the needed. 
        if($type == "open") {//The starting of the tag ‘<tag>‘ 
            $parent[$level-1] = &$current; 
             if(!is_array($current) or (!in_array($tag, array_keys($current)))) { //Insert New tag 
                $current[$tag] = $result; 
                 if($attributes_data) $current[$tag. ‘_attr‘] = $attributes_data; 
                $repeated_tag_index[$tag.‘_‘.$level] = 1;

$current = &$current[$tag];

} else { //There was another element with the same tag name

if(isset($current[$tag][0])) {//If there is a 0th element it is already an array 
                    $current[$tag][$repeated_tag_index[$tag.‘_‘.$level]] = $result; 
                    $repeated_tag_index[$tag.‘_‘.$level]++; 
                 } else {//This section will make the value an array if multiple tags with the same name appear together 
                    $current[$tag] = array($current[$tag],$result);//This will combine the existing item and the new item together to make an array 
                    $repeated_tag_index[$tag.‘_‘.$level] = 2; 
                      
                     if(isset($current[$tag.‘_attr‘])) { //The attribute of the last(0th) tag must be moved as well 
                        $current[$tag][‘0_attr‘] = $current[$tag.‘_attr‘]; 
                         unset($current[$tag.‘_attr‘]); 
                     }


                $last_item_index = $repeated_tag_index[$tag.‘_‘.$level]-1; 
                $current = &$current[$tag][$last_item_index]; 
             }

} elseif($type == "complete") { //Tags that ends in 1 line ‘<tag />‘ 
             //See if the key is already taken. 
            if(!isset($current[$tag])) { //New Key 
                $current[$tag] = $result; 
                $repeated_tag_index[$tag.‘_‘.$level] = 1; 
                 if($priority == ‘tag‘ and $attributes_data) $current[$tag. ‘_attr‘] = $attributes_data;

} else { //If taken, put all things inside a list(array) 
                if(isset($current[$tag][0]) and is_array($current[$tag])) {//If it is already an array...

// ...push the new element into that array. 
                    $current[$tag][$repeated_tag_index[$tag.‘_‘.$level]] = $result; 
                      
                     if($priority == ‘tag‘ and $get_attributes and $attributes_data) { 
                        $current[$tag][$repeated_tag_index[$tag.‘_‘.$level] . ‘_attr‘] = $attributes_data; 
                     } 
                    $repeated_tag_index[$tag.‘_‘.$level]++;

} else { //If it is not an array... 
                    $current[$tag] = array($current[$tag],$result); //...Make it an array using using the existing value and the new value 
                    $repeated_tag_index[$tag.‘_‘.$level] = 1; 
                     if($priority == ‘tag‘ and $get_attributes) { 
                         if(isset($current[$tag.‘_attr‘])) { //The attribute of the last(0th) tag must be moved as well 
                              
                            $current[$tag][‘0_attr‘] = $current[$tag.‘_attr‘]; 
                             unset($current[$tag.‘_attr‘]); 
                         } 
                          
                         if($attributes_data) {2881064151 
                            $current[$tag][$repeated_tag_index[$tag.‘_‘.$level] . ‘_attr‘] = $attributes_data; 
                         } 
                     } 
                    $repeated_tag_index[$tag.‘_‘.$level]++; //0 and 1 index is already taken 
                } 
             }

} elseif($type == ‘close‘) { //End of tag ‘</tag>‘ 
            $current = &$parent[$level-1]; 
         } 
     } 
      
     return($xml_array); 

?>

函数描述及例子

$arr = xml2array(file_get_contents("tools.xml"),1,‘attribute‘);

查询关键字

时间: 2024-10-14 04:13:13

php函数描述及例子的相关文章

【c++】虚函数描述符override

在C++11中为了帮助程序员写继承结构复杂的类型,引入了虚函数描述符override,如果派生类在虚函数声明时使用了override描述符,那么该函数必须重载其基类中的同名函数,否则代码将无法通过编译.我们来看一下如代码清单2-25所示的这个简单的例子. 代码清单2-25 struct Base { virtual void Turing() = 0; virtual void Dijkstra() = 0; virtual void VNeumann(int g) = 0; virtual v

PHP开发常用函数和必须会的函数总结 --带例子

PHP开发常用函数 在共过开发中,我经常要用到一些PHP函数,有时会不记得,这里把这些常用函数总结到一起.本篇列举了PHP程序员开发中经常用到的php函数.几乎每个函数都有例子,老手可以看看当做温习,新手可以背理论,然后把例子敲他几遍,后面再开发中,开发效率绝对有所提高.例子大多取自w3cschool标准例子和网上的一些资源,部分为个人所写,均经过测试.如果要装载本文,还请注明来源 :http://blog.csdn.net/qq_22327455. 一.检查函数 1.empty() empty

一个完整的使用成员函数指针的例子

Screen.h #ifndef SCREEN_H #define SCREEN_H #include <string> class Screen { public: typedef std::string::size_type pos; // Action is a type that can point to a member function of Screen // that returns a reference to a Screen and takes no arguments

C API函数描述(A-F)

25.2.3.1. mysql_affected_rows() my_ulonglong mysql_affected_rows(MYSQL *mysql) 描述 返回上次UPDATE更改的行数,上次DELETE删除的行数,或上次INSERT语句插入的行数.对于UPDATE.DELETE或INSERT语句,可在mysql_query()后立刻调用.对于SELECT语句,mysql_affected_rows()的工作方式与mysql_num_rows()类似. 返回值 大于0的整数表明受影响或检

C API函数描述(G-N)

25.2.3.26. mysql_get_character_set_info() void mysql_get_character_set_info(MYSQL *mysql, MY_CHARSET_INFO *cs) 描述 该函数提供了关于默认客户端字符集的信息.可以使用mysql_set_character_set()函数更改默认的字符集. 该函数是在MySQL 5.0.10中增加的. 示例: if (!mysql_set_character_set(&mysql, "utf8&q

C API函数描述(O-R)

25.2.3.48. mysql_options() int mysql_options(MYSQL *mysql, enum mysql_option option, const char *arg) 描述 可用于设置额外的连接选项,并影响连接的行为.可多次调用该函数来设置数个选项. 应在mysql_init()之后.以及mysql_connect()或mysql_real_connect()之前调用mysql_options(). 选项参量指的是你打算设置的选项.Arg参量是选项的值.如果选

C API函数描述(S-W)

25.2.3.59. mysql_select_db() int mysql_select_db(MYSQL *mysql, const char *db) 描述 使由db指定的数据库成为由mysql指定的连接上的默认数据库(当前数据库).在后续查询中,该数据库将是未包含明确数据库区分符的表引用的默认数据库. 除非已连接的用户具有使用数据库的权限,否则mysql_select_db()将失败. 返回值 0表示成功,非0值表示出现错误. 错误 ·        CR_COMMANDS_OUT_OF

OpenGL glMatrixMode() 函数解释与例子

概述 glMatrixMode() 用以指定当前要操作的矩阵,可选值有 GL_MODELVIEW(模型视图,默认值),GL_PROJECTION(投影),GL_TEXTURE(纹理),GL_COLOR(颜色) 当我们使用 glMatrixMode(GL_MODELVIEW) 时,即表示我们接下来要对模型视图矩阵堆栈进行一些操作(Applies subsequent matrix operations to the modelview matrix stack),例如位移,旋转,缩放,亦或是是要添

《coredump问题原理探究》Linux x86版6.5节虚函数的coredump例子

在大型项目中,很容易出现版本不匹配的问题,其中导致的虚函数飘移的问题比较难解决. 在这里,用一个例子来说明如何解决这种问题. 建立三个源文件:testso.h,testso.cpp,xuzhina_dump_c6_s3_ex.cpp. testso.h的代码如下: 1 #ifndef __TESTSO_H__ 2 #define __TESTSO_H__ 3 4 class xuzhina_dump_c6_s3_ex 5 { 6 public: 7 virtual char* encode( c