Of course, PHP has many ways to output arrays.
First, we can use the print_r() function to print the array:
$arr = [1, 2, 3, 4, 5]; print_r($arr);
The output result is:
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 )
Among them, Array indicates that this is an array type variable, brackets Inside are the subscript and value of each element.
In addition, we can also use the var_dump() function to print the array in more detail:
$arr = [1, 2, 3, 4, 5]; var_dump($arr);
The output result is:
array(5) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(4) [4]=> int(5) }
As you can see, var_dump( ) function will output not only the index and value of each element, but also the data type and length of the value.
In addition to the above two functions, you can also use echo statements and loop structures to output arrays. For example:
$arr = [1, 2, 3, 4, 5]; foreach ($arr as $value) { echo $value . ' '; }
The output result is:
1 2 3 4 5
It should be noted that when using the echo statement to output an array, the array will be converted to a string type and each element will be separated by spaces.
In addition, you can also use the implode() function to convert the array into a string output:
$arr = [1, 2, 3, 4, 5]; $str = implode(' ', $arr); echo $str;
The output result is also:
1 2 3 4 5
In short, it is very convenient to output arrays in PHP , just choose different methods according to your needs.
The above is the detailed content of How to output array array in php? A brief analysis of various methods. For more information, please follow other related articles on the PHP Chinese website!