PHP处理数组和XML之间的互相转换,php处理数组xml
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; }
我测试了下,这个最简单,速度又快,支持多为数组,中文也不会乱码。
另一种方法是利用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)?$key:"item"); $item->appendChild($itemx); if (!is_array($val)){ $text = $dom->createTextNode($val); $itemx->appendChild($text); }else { arrayToXml($val,$dom,$itemx); } } return $dom->saveXML(); }
它同样可以将数组转换成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>
通过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; }
调用xmlToArray()可得到以下结果:
得到数组后,我们就可以对数据进行各种处理了。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持帮客之家。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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

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

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

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

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

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

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

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
