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;
Output result:
Array
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);
Output result:
Array ( [0] => apple [1] => orange [2] => banana [3] => peach [4] => grape )
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);
Output result:
array(5) { [0]=> string(5) "apple" [1]=> string(6) "orange" [2]=> string(6) "banana" [3]=> string(5) "peach" [4]=> string(5) "grape" }
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:
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 "";
Output result:
Array ( [0] => apple [1] => orange [2] => banana [3] => peach [4] => grape )
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));
Output results:
Array ( [0] => apple [1] => orange [2] => banana [3] => peach [4] => grape )
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>"; }
Output result:
apple orange banana peach grape
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);
Output results:
[ "apple", "orange", "banana", "peach", "grape" ]
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!