Convert XML and arrays
Two methods recursive conversion array and xml
<?php /*simplexml数组和xml转换*/ // 对象转数组:为了更好地找到需要的节点 function toarray($sim){ // 1 先把最外面的object变成数组 $sim=(array)$sim; //如果是对象或者数组,就都递归,运用上面的转变,如果不是这两者之一就pass foreach ($sim as $k => $v) { if ($v instanceof simplexmlelement||is_array($v)) { $sim[$k]=toarray($v); } } return $sim; } $sx=simplexml_load_file('./try.xml'); print_r($sx); print_r(toarray($sx)); // 看源代码可以看到,网页返回是报错 $arr=array('a'=>'111', 'b'=>'2222', 'c'=>array( 'd'=>'hahaha', 'e'=>array( 'what'=>'the x'))); // 数组转xml,新的数组加入到新xml里,,arr to xml function toxml($arr,$node=null){ // 创建新xml文件 if ($node==null) { $simxml=new simpleXMLElement ('<?xml version="1.0" encoding="utf-8"?><root></root>'); }else{ $simxml=$node; } // foreach ($arr as $k => $v) { if (is_array($v)) { // addChild(节点名,内容) // 如果是数组,就要建节点 把这个数组里的东西放进去--》v是个数组, // 大节点名是k,到下一回,把东西放进这个sim-》addchild里 toxml($v,$simxml->addChild($k)); //如果数组键是数字,就要自己给他个标签名,因为直接用数字 会出问题 }else if (is_numeric($k)) { $simxml->addChild('item',$v); }else{ // 不是数组就直接添加子元素 键名-键值 $simxml->addChild($k,$v); } } return $simxml->saveXML(); } header('content-type:text/xml'); echo toxml($arr); ?>
The above has introduced the conversion of XML and arrays, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

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

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

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











Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

Can XML files be opened with PPT? XML, Extensible Markup Language (Extensible Markup Language), is a universal markup language that is widely used in data exchange and data storage. Compared with HTML, XML is more flexible and can define its own tags and data structures, making the storage and exchange of data more convenient and unified. PPT, or PowerPoint, is a software developed by Microsoft for creating presentations. It provides a comprehensive way of

Convert XML data in Python to CSV format XML (ExtensibleMarkupLanguage) is an extensible markup language commonly used for data storage and transmission. CSV (CommaSeparatedValues) is a comma-delimited text file format commonly used for data import and export. When processing data, sometimes it is necessary to convert XML data to CSV format for easy analysis and processing. Python is a powerful

Handling Errors and Exceptions in XML Using Python XML is a commonly used data format used to store and represent structured data. When we use Python to process XML, sometimes we may encounter some errors and exceptions. In this article, I will introduce how to use Python to handle errors and exceptions in XML, and provide some sample code for reference. Use try-except statement to catch XML parsing errors When we use Python to parse XML, sometimes we may encounter some

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

Python parses special characters and escape sequences in XML XML (eXtensibleMarkupLanguage) is a commonly used data exchange format used to transfer and store data between different systems. When processing XML files, you often encounter situations that contain special characters and escape sequences, which may cause parsing errors or misinterpretation of the data. Therefore, when parsing XML files using Python, we need to understand how to handle these special characters and escape sequences. 1. Special characters and

How to handle XML and JSON data formats in C# development requires specific code examples. In modern software development, XML and JSON are two widely used data formats. XML (Extensible Markup Language) is a markup language used to store and transmit data, while JSON (JavaScript Object Notation) is a lightweight data exchange format. In C# development, we often need to process and operate XML and JSON data. This article will focus on how to use C# to process these two data formats, and attach

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an
