How to wrap line when printing array in php

WBOY
Release: 2023-05-19 19:53:36
Original
1856 people have browsed it

In PHP, we can use a variety of methods to print arrays, and we can choose to output them to the browser screen or a text file. In most cases, we use echo or print functions to output arrays, but when outputting large arrays, the output results may become a bit confusing.

Therefore, we need to use functions and techniques for formatting output to make the output easier to read and diagnose. In this article, we will learn how to use these functions and techniques to print an array and make it easy to read.

1. Print array

When we use the echo or print function to output an array, PHP will automatically convert it into a string. The string representation of an array is surrounded by curly braces and commas separate the array elements. However, this representation can get a bit messy when the array is large. Here is a simple example:

$arr = array("apple", "orange", "banana", "peach", "grape");
echo $arr;
Copy after login

Output result:

Array
Copy after login

This is not the result we want because this is just the type of the array. To print the entire array, we need to use the print_r function. The print_r function can print more detailed information about the array, including keys and values. This is a simple example:

$arr = array("apple", "orange", "banana", "peach", "grape");
print_r($arr);
Copy after login

Output result:

Array
(
    [0] => apple
    [1] => orange
    [2] => banana
    [3] => peach
    [4] => grape
)
Copy after login
Copy after login
Copy after login

Pay attention to the format of the output result. When using the print_r function, the array elements are printed sequentially on a new line, with each array element including the key and value. In the output, each new line is preceded by an indent, which makes the elements in the array very easy to read and read.

2. Use the var_dump function

Another useful function is the var_dump function. This function is somewhat similar to print_r, but it provides more detailed and comprehensive output. The var_dump function displays not only the keys and values ​​of the array, but also their data type and length. An example of using the var_dump function is as follows:

$arr = array("apple", "orange", "banana", "peach", "grape");
var_dump($arr);
Copy after login

Output result:

array(5) {
  [0]=>
  string(5) "apple"
  [1]=>
  string(6) "orange"
  [2]=>
  string(6) "banana"
  [3]=>
  string(5) "peach"
  [4]=>
  string(5) "grape"
}
Copy after login

From the above output result, we can easily see the length and data type of the array data, which is useful when debugging the code very useful.

3. Formatted output

If the array is large, the results printed by using the print_r or var_dump function may become a bit confusing and not suitable for reading. Fortunately, there are some tricks and functions we can use to format the output and make the array easier to read. Here are some examples:

  1. Use pre tag

Placing the output results in the pre tag of HTML can preserve the format of the output results, making it easy to read and diagnose .

$arr = array("apple", "orange", "banana", "peach", "grape");
echo "<pre class="brush:php;toolbar:false">";
print_r($arr);
echo "
";
Copy after login

Output result:

Array
(
    [0] => apple
    [1] => orange
    [2] => banana
    [3] => peach
    [4] => grape
)
Copy after login
Copy after login
Copy after login
  1. Use nl2br function

Another method is to use nl2br function. The nl2br function inserts an HTML newline character after each new line in the string, making the array easier to read.

$arr = array("apple", "orange", "banana", "peach", "grape");
echo nl2br(print_r($arr, true));
Copy after login

Output results:

Array
(
    [0] => apple
    [1] => orange
    [2] => banana
    [3] => peach
    [4] => grape
)
Copy after login
Copy after login
Copy after login
  1. Using foreach loop

Using foreach loop allows us to flexibly control the output format of the array, especially when we When you need to output complex or large multi-dimensional arrays.

$arr = array("apple", "orange", "banana", "peach", "grape");
foreach ($arr as $value) {
    echo $value . "<br>";
}
Copy after login

Output result:

apple
orange
banana
peach
grape
Copy after login
  1. Use json_encode function

json_encode function can convert the array into a json string, which can make the array easier to read Fetch and manage.

$arr = array("apple", "orange", "banana", "peach", "grape");
echo json_encode($arr, JSON_PRETTY_PRINT);
Copy after login

Output results:

[
    "apple",
    "orange",
    "banana",
    "peach",
    "grape"
]
Copy after login

Summary

By using print_r, var_dump, pre tag, nl2br function, foreach loop and json_encode function, we can easily print the array and encode it The array is output in a format that makes it easy to read and manage. These techniques and functions are useful, especially when working with large or complex arrays.

The above is the detailed content of How to wrap line when printing 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