Home > Article > Backend Development > How to print array in php
In PHP, printing an array is a basic operation. Array is a very convenient data structure that can store any type of data. If you don't know how to print an array, you have no way of knowing what data is stored in the array. In this article, we will learn how to print an array in PHP.
1. Use the var_dump function to print an array
In PHP, use the var_dump() function to print out an array. The var_dump() function can output detailed information about any type of variable, including type, length, value, etc. The following is a simple example:
$arr = array('apple', 'banana', 'orange'); var_dump($arr);
Running results:
array(3) { [0]=> string(5) "apple" [1]=> string(6) "banana" [2]=> string(6) "orange" }
From the results, we can see that when printing an array, the var_dump() function will print out each element of the array. , enclosed in square brackets that contain the index value of the element. Additionally, each element is preceded by its type and length.
2. Use the print_r function to print an array
In addition to the var_dump() function, PHP also provides the print_r() function to print an array. Unlike the var_dump() function, the print_r() function only prints out the value of the array and does not include type information and length information. The following is an example:
$arr = array('apple', 'banana', 'orange'); print_r($arr);
Running results:
Array ( [0] => apple [1] => banana [2] => orange )
As can be seen from the results, the print_r() function prints out each element in the array. Unlike var_dump(), the print_r() function only represents array elements by their index, without displaying type and length information.
3. Traverse the array
In addition to using the var_dump() and print_r() functions to print the array, you can also use a loop to traverse the array and print its elements. Here is a simple example:
$arr = array('apple', 'banana', 'orange'); for ($i=0; $i<count($arr); $i++) { echo $arr[$i]; echo '<br>'; }
Running results:
apple banana orange
As you can see from the results, by looping through the array, we can access each element and print it out. In this example, we used a for loop and used the count() function to determine the length of the array.
4. Use foreach loop to traverse arrays
PHP also provides a simpler way to traverse arrays: use foreach loop. The foreach loop can automatically iterate through each element in the array and store it in a temporary variable that we can use to process the array elements. The following is an example of using a foreach loop to traverse an array:
$arr = array('apple', 'banana', 'orange'); foreach ($arr as $value) { echo $value; echo '<br>'; }
Running results:
apple banana orange
From the results we can see that when using a foreach loop, we store each element of the array in a temporary variable $value and use that variable to process array elements. Unlike the for loop, we don't need to use the count() function to determine the length of the array.
Summary
In this article, we introduced the different ways to print arrays in PHP, including the var_dump() function, print_r() function, for loop, and foreach loop. These methods help us understand the data stored in the array. Printing an array is an essential operation when we need to debug and write code. No matter which method you choose, mastering how to print arrays will increase your efficiency and accuracy in PHP programming.
The above is the detailed content of How to print array in php. For more information, please follow other related articles on the PHP Chinese website!