Home  >  Article  >  Backend Development  >  How to format array in php

How to format array in php

WBOY
WBOYOriginal
2023-05-19 15:52:111166browse

In the PHP language, array is a very important data structure used to store and operate large amounts of data. For array data, it is often necessary to format it into a specific form for display or transmission. This article will introduce how to format arrays in PHP.

1. Convert the array to JSON format

JSON is a lightweight data exchange format that is often used for data transmission between the front and back ends. In PHP, you can use the json_encode() function to convert an array into JSON format. The sample code is as follows:

<?php
$arr = array('name' => 'John', 'age' => 30, 'city' => 'New York');
$json = json_encode($arr);
echo $json;
?>

The output result is as follows:

{"name":"John","age":30,"city":"New York"}

2. Convert the array to XML format

XML is a markup language commonly used for data storage and transmission. In PHP, you can use the SimpleXML class to convert an array into XML format. The sample code is as follows:

<?php
$arr = array('name' => 'John', 'age' => 30, 'city' => 'New York');
$xml = new SimpleXMLElement('<root/>');
array_walk_recursive($arr, array($xml, 'addChild'));
echo $xml->asXML();
?>

The output result is as follows:

<?xml version="1.0"?>
<root>
  <name>John</name>
  <age>30</age>
  <city>New York</city>
</root>

3. Convert the array to CSV format

CSV is a comma-delimited file format commonly used for storage and transfer form data. In PHP, you can use the fputcsv() function to convert an array to CSV format. The sample code is as follows:

<?php
$arr = array(
    array('name', 'age', 'city'),
    array('John', 30, 'New York'),
    array('Mary', 25, 'Los Angeles'),
    array('Tom', 35, 'Chicago')
);
$fp = fopen('data.csv', 'w');
foreach ($arr as $fields) {
    fputcsv($fp, $fields);
}
fclose($fp);
?>

4. Convert the array to HTML table format

HTML table is a common data display form and is often used in web pages. In PHP, you can use loop statements and HTML tags to convert arrays into HTML table format. The sample code is as follows:

<?php
$arr = array(
    array('name', 'age', 'city'),
    array('John', 30, 'New York'),
    array('Mary', 25, 'Los Angeles'),
    array('Tom', 35, 'Chicago')
);
echo '<table>';
foreach ($arr as $row) {
    echo '<tr>';
    foreach ($row as $col) {
        echo '<td>' . $col . '</td>';
    }
    echo '</tr>';
}
echo '</table>';
?>

The above are common methods for formatting arrays. For different needs, you can also choose other formatting methods according to specific circumstances. It should be noted that during the formatting process, attention should be paid to the type and format of the data to ensure the correctness and readability of the data.

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

Statement:
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