Table of Contents
PHP处理数组和XML之间的互相转换,php处理数组xml
Home php教程 php手册 PHP处理数组和XML之间的互相转换,php处理数组xml

PHP处理数组和XML之间的互相转换,php处理数组xml

Jun 13, 2016 am 08:38 AM
php xml object array array

PHP处理数组和XML之间的互相转换,php处理数组xml

在开发中,我们经常会遇到数组与XML之间的互相转换,尤其在处理接口开发的时候经常用到,比如对方客户端POST一个XML格式的数据到服务器上,服务器上的程序要负责接收解析,还有需要将数据表数据以XML格式提供给第三方等等应用。
本文我们将简单介绍如何使用PHP处理数组和XML之间的互相转换。

源码下载:PHP数组与XML之间的转换

PHP将数组转换成XML
 PHP可以将数组转换成xml格式,简单的办法是遍历数组,然后将数组的key/value转换成xml节点,再直接echo输出了,如:

function arrayToXml($arr){ 
$xml = "<root>"; 
foreach ($arr as $key=>$val){ 
if(is_array($val)){ 
$xml.="<".$key.">".arrayToXml($val)."</".$key.">"; 
}else{ 
$xml.="<".$key.">".$val."</".$key.">"; 
} 
} 
$xml.="</root>"; 
return $xml; 
}
Copy after login

我测试了下,这个最简单,速度又快,支持多为数组,中文也不会乱码。
另一种方法是利用DOMDocument来生成xml结构:

function arrayToXml($arr,$dom=0,$item=0){ 
if (!$dom){ 
$dom = new DOMDocument("1.0"); 
} 
if(!$item){ 
$item = $dom->createElement("root"); 
$dom->appendChild($item); 
} 
foreach ($arr as $key=>$val){ 
$itemx = $dom->createElement(is_string($key)&#63;$key:"item"); 
$item->appendChild($itemx); 
if (!is_array($val)){ 
$text = $dom->createTextNode($val); 
$itemx->appendChild($text); 

}else { 
arrayToXml($val,$dom,$itemx); 
} 
} 
return $dom->saveXML(); 
} 
Copy after login

它同样可以将数组转换成xml,而且支持多维数组,生成的xml中文也不会乱码。

PHP将XML转换成数组
做接口开发的时候经常会碰到别人提交给你的是xml格式的数据,常见的微信接口、支付宝接口等,他们的接口如发送消息通信都是xml格式的,那么我们先想办法拿到这个xml数据,然后再将其转化成数组。
假设我们获取到一个这样的XML:

 <root> 

<user>

月光光abcd</user> 

<pvs>13002</pvs>

 <ips> 

<baidu_ip>1200</baidu_ip>

 <google_ip>1829</google_ip>

 </ips> 

<date>2016-06-01</date>

 </root> 

Copy after login

通过simplexml_load_string()解析读取xml数据,然后先转成json格式,再转换成数组。

 function xmlToArray($xml){ 

 //禁止引用外部xml实体 

libxml_disable_entity_loader(true); 

$xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA); 

$val = json_decode(json_encode($xmlstring),true); 

return $val; 

} 
Copy after login

调用xmlToArray()可得到以下结果:

 

得到数组后,我们就可以对数据进行各种处理了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持帮客之家。

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
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)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

CakePHP Logging CakePHP Logging Sep 10, 2024 pm 05:26 PM

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

See all articles