Home > Article > Backend Development > How to output an array in php
Similar to the creation of an array, there are three ways to display the output of an array.
1. for loop (recommended learning: PHP video tutorial)
<?php $ms_office = array( 'word', 'excel', 'outlook', 'access' ); for($i=0; $i<4; $i++) { echo "数组第".($i+1)."个元素是:"; echo $ms_office[$i]; echo "<br>"; } ?>
数组第1个元素是:word 数组第2个元素是:excel 数组第3个元素是:outlook 数组第4个元素是:access
Suddenly One of the interesting things about PHP is that after it uses $ to define a variable, no matter where you go, as long as you use the variable, the variable name will be preceded by $.
2. Use the foreach loop statement
<?php $ms_office = array( 'word', 'excel', 'outlook', 'access' ); foreach($ms_office as $software) { echo $software; echo "<br>"; } ?>
word excel outlook access
defines an array without specifying its index, so the default integer form is used.
3. Use print_r() to display array elements
<?php $ms_office = array( 'word', 'excel', 'outlook', 'access' ); print_r($ms_office); ?>
Array ( [0] => word [1] => excel [2] => outlook [3] => access )
Add echo "e03b848252eb9375d56be284e690e873"; in front of print_r() to get an array structure with a clearer format.
You can also add code to output the end tag "bc5574f69a0cba105bc93bd3dc13c4ec" after print_r().
Array ( [0] => word [1] => excel [2] => outlook [3] => access )
HTML tag e03b848252eb9375d56be284e690e873 can define pre-formatted text. e03b848252eb9375d56be284e690e873bc5574f69a0cba105bc93bd3dc13c4ecYou can display the spaces, carriage returns, line feeds, and Tab keys in the text between them, that is, display them according to the original layout of the text.
The above is the detailed content of How to output an array in php. For more information, please follow other related articles on the PHP Chinese website!