Home > Article > Backend Development > How to output the value of an array in php page
In PHP, array is a very common data type, and outputting the value of the array is one of the things that is often done in development. In this article, we will introduce several basic ways to output the value of a PHP array, as well as some more extensible methods.
Method 1: print_r function
The print_r() function is the most basic method of outputting array values in PHP. It displays variable information in a readable format and is often used during debugging and development.
Using this function to output the value of an array is very simple. Just pass in the array variable to be output, as shown in the following example:
$array = array('apple', 'banana', 'orange'); print_r($array);
The output result is as follows:
Array ( [0] => apple [1] => banana [2] => orange )
Each element of the output will be displayed, and each element and its corresponding index will be listed in order.
Method 2: var_dump function
Similar to print_r(), var_dump() is also a common way to output array values. var_dump() displays structural information for one or more expressions, including their type and value, and in the case of an array, all elements along with their type and value.
It is also very simple to use this function to output the value of the array. You only need to pass in the array variable to be output as a parameter, as shown in the following example:
$array = array('apple', 'banana', 'orange'); var_dump($array);
The output result is as follows:
array(3) { [0]=> string(5) "apple" [1]=> string(6) "banana" [2]=> string(6) "orange" }
As you can see, the output results are relatively detailed, including information such as the value and type of the array elements.
Method 3: foreach statement
If you need to output each element of the array, you can use the foreach statement to help traverse the array, as shown below:
$array = array('apple', 'banana', 'orange'); foreach ($array as $value) { echo $value . "<br>"; }
The output result is as follows Display:
apple banana orange
You can see that this method can output each element of the array, that is, the '$value' variable.
Method 4: for statement
Through the for statement, each element of the array and its index value can also be output. You can get the array length (using the count() function) and loop through the values and index values in the array, as shown below:
$array = array('apple', 'banana', 'orange'); for ($i = 0; $i < count($array); $i++) { echo "Index " . $i . ": " . $array[$i] . "<br>"; }
The output result is as follows:
Index 0: apple Index 1: banana Index 2: orange
With using foreach Compared with the statement, this method can output the index value of each element and the corresponding array element value. The details can be adjusted as needed.
Method 5: array_values function
When the array uses string keys, if you need to output the array by value. You can use the array_values() function. This function returns all values in the array without retaining the keys.
An example of using this function is as follows:
$array = array("a" => "apple", "b" => "banana", "c" => "orange"); $values = array_values($array); print_r($values);
The output result is as follows:
Array ( [0] => apple [1] => banana [2] => orange )
As you can see, the output result does not contain the key name information, only the key name information. the value of the array element.
To sum up, the above are several common methods for PHP to output array values. According to needs, we can flexibly choose the method that suits us.
The above is the detailed content of How to output the value of an array in php page. For more information, please follow other related articles on the PHP Chinese website!