How to output array array in php? A brief analysis of various methods

PHPz
Release: 2023-04-25 09:37:43
Original
1401 people have browsed it

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);
Copy after login

The output result is:

Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 )
Copy after login

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);
Copy after login

The output result is:

array(5) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(4) [4]=> int(5) }
Copy after login

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 . ' '; }
Copy after login

The output result is:

1 2 3 4 5
Copy after login
Copy after login

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;
Copy after login

The output result is also:

1 2 3 4 5
Copy after login
Copy after login

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!

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
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!