Home > Backend Development > PHP Tutorial > Discussion: array2xml and xml2array and the mutual conversion of xml and array_PHP tutorial

Discussion: array2xml and xml2array and the mutual conversion of xml and array_PHP tutorial

WBOY
Release: 2016-07-21 15:04:15
Original
922 people have browsed it

When PHP is working as a back-end server, it often encounters this situation. It needs to parse the xml file from the front-end and return the data in xml format. In this case, the conversion between xml and the associative array in PHP is Very frequent thing. For example, flex and other client programs often use this method to interact with the server. The following are two methods I have summarized, which greatly simplify the workload of converting xml and array.

Copy code The code is as follows:

/**
     *
     * 将简单数组转化为简单的xml
     * @param string $data  要进行转化的数组
     * @param string $tag   要使用的标签
     * @example
     * $arr = array(
        'rtxAccount'=>'aaron','ipAddr'=>'192.168.0.12',
        'conferenceList'=>array('conference'=>
                            array(
                                array('conferenceId'=>1212,'conferenceTitle'=>'quanshi 444','smeAccount'=>'http://www.jb51.net'),
                                array('conferenceId'=>454,'conferenceTitle'=>'quanshi meetting','smeAccount'=>'http://www.jb51.net'),
                                array('conferenceId'=>6767,'conferenceTitle'=>'quanshi meetting','smeAccount'=>'http://www.jb51.net'),
                                array('conferenceId'=>232323,'conferenceTitle'=>'quanshi uuu','smeAccount'=>'http://www.jb51.net'),
                                array('conferenceId'=>8989,'conferenceTitle'=>'quanshi meetting','smeAccount'=>'http://www.jb51.net'),
                                array('conferenceId'=>1234343212,'conferenceTitle'=>'quanshi meetting','smeAccount'=>'http://www.jb51.net')
                                )
                            )
        );
        转化为:
        aaron
        192.168.0.12
       
           
                1212
                quanshi 444
                http://www.jb51.net
           

           
                454
                quanshi meetting
                http://www.jb51.net
           

           
                6767
                quanshi meetting
                http://www.jb51.net
           

           
                232323
                quanshi uuu
                http://www.jb51.net
           

           
                8989
                quanshi meetting
                http://www.jb51.net
           

           
                1234343212
                quanshi meetting
                http://www.jb51.net
           

       

     */
    function array2xml($data,$tag = '')
    {
        $xml = '';

        foreach($data as $key => $value)
        {
            if(is_numeric($key))
            {
                if(is_array($value))
                {
                    $xml .= "<$tag>";
                    $xml .= array2xml($value);
                    $xml .="";
                }
                else
                {
                    $xml .= "<$tag>$value";
                }   
            }
            else
            {
                if(is_array($value))
                {
                    $keys = array_keys($value);
                    if(is_numeric($keys[0]))
                    {
                        $xml .=array2xml($value,$key);
                    }
                    else
                    {
                        $xml .= "<$key>";
                        $xml .=array2xml($value);
                        $xml .= "";
                    }

                }
                else
                {
                    $xml .= "<$key>$value";
                }
            }
        }
        return $xml;
    }            
}

xml2array
复制代码 代码如下:

/**
*
* Convert simple xml into associative array
* @param string $xmlString xml string
* @example
*

IT Exchange Conference
2011-12-19 12:00: 00
andy1111111
192.168.1.56
120



Invitee 1’s RTX account
Invitee 1 Phone number


Invitee 2’s RTX account
Invited Person 2 phone number



Associative array after conversion:
Array
(
[conferenceTitle] => IT Exchange Meeting
[startTime] => 2011-12-19 12:00:00
[rtxAccount] => andy1111111
[ipAddr] => 192.168.1.56
[duration] => 120
[conferenceType] => 1
[invitees] => Array
(
[invitee] => Array
(> 🎜>                                                                                                                                                                                                                                                                                  
[1] = & gt; Array
(
[🎜> [rtxaccount] = & gt; the RTX account number of the invited person 2
[tel] = & gt; 🎜>          )
     ) 
)
*/
 function xml2array($xmlString = '')
 {
  $targetArray = array();
  $xmlObject = simplexml_load_string($xmlString);
  $mixArray = (array)$xmlObject;
  foreach($mixArray as $key => $value)
  {
   if(is_string($value))
   {
    $targetArray[$key] = $value;
   }
   if(is_object($value))
   {
    $targetArray[$key] = xml2array($value->asXML());
   }
   if(is_array($value))
   {
    foreach($value as $zkey => $zvalue)
    {
     if(is_numeric($zkey))
     {
      $targetArray[$key][] = xml2array($zvalue->asXML());
     }
if(is_string($zkey))
{
$targetArray[$key][$zkey] = xml2array($zvalue->asXML());
}
}
}
}
return $targetArray;

}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327794.htmlTechArticleWhen PHP is working as a backend server, it often encounters this situation and needs to parse the xml file from the frontend. And return the data in xml format, in this case, xml is associated with the array in php...
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template