How to convert accessed XML file into array in php

PHPz
Release: 2023-04-26 15:12:19
Original
477 people have browsed it

In the process of using PHP for web development, we often need to operate XML files. Data in XML files usually need to be converted into arrays for processing in programs. This article will introduce how to convert the accessed XML file into an array.

1. Introduction

XML (Extensible Markup Language) is a language for storing and exchanging structured data. It is very versatile and can be used in a variety of different environments and applications. You can easily read, edit and generate XML documents using PHP. But in actual development, we often need to convert XML data into arrays for easy use in programs. This article will use a practical example to illustrate how to convert an XML file into an array and operate it.

2. Example

In this example, we will use an XML file (weather forecast) provided by the Hong Kong Observatory and convert it into an array. We can obtain this XML file at this website: http://rss.weather.gov.hk/rss/CurrentWeather/city.xml. Download this file and save it locally.

3. Code Analysis

We first need to load this XML file. XML files can be easily loaded and converted into PHP objects using the simplexml_load_file() function.

$xmlObj = simplexml_load_file("city.xml");

Next, we will use object mode to access the data in this XML file and save them to an array:

$cityArray = array();
foreach ($xmlObj->channel->item as $item) {

$titleArray = explode(" / ", $item->title);
$city = $titleArray[0];
$temp = substr($titleArray[1], 0, 2);
$cityArray[$city] = array("temperature" => $temp);
Copy after login
Copy after login

}

In this code, We first looped through all the cities in the XML file. For each city, we save the data into an array in the form of "city name => temperature".

Next, we can use the data in this array to perform any required operations. For example, we can output the temperature of all cities:

foreach ($cityArray as $city => $data) {

echo "$city 的温度为:{$data['temperature']}度。";
Copy after login
Copy after login

}

4. Complete code

The following is the complete code, including the process of reading the XML file and converting it into an array and outputting the temperature of all cities:

$xmlObj = simplexml_load_file("city.xml");
$cityArray = array();
foreach ($xmlObj->channel->item as $item) {

$titleArray = explode(" / ", $item->title);
$city = $titleArray[0];
$temp = substr($titleArray[1], 0, 2);
$cityArray[$city] = array("temperature" => $temp);
Copy after login
Copy after login

}

foreach ($cityArray as $city => $data) {

echo "$city 的温度为:{$data['temperature']}度。";
Copy after login
Copy after login

}

5. Summary

This article introduces how to convert XML files into arrays. Using PHP's simplexml_load_file() function, we can easily load and convert XML files into PHP objects. Then we can use the object method to access and manipulate the data in this XML file, and save them into an array for easy use in the program. This method is very convenient and can improve our development efficiency.

The above is the detailed content of How to convert accessed XML file into array in php. For more information, please follow other related articles on the PHP Chinese website!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!