The method for PHP to output array elements
often used to access the values of arrays in actual development. It is very cumbersome if each element is indexed by array index. In
PHP, you can quickly access array elements through various loops. This section introduces the common for and foreach.
uses an for loop statement to output array elements for an array that is indexed in an integer order, which can be accessed by an array element,
[code]
$ms_office = array (
'word',
'excel',
echo $ms_office.#91; $i.#93;
echo.Quot; The grammatical structure of a ring statement is shown as follows.
[code]foreach ($array as $some_var)
{
statement
}[/code]
foreach and as are the keyword of the extension. The meaning of this grammatical structure is that, in order, every time a meta
element in the array $array is stored in the variable $some_var, the statement statement is executed, then the next element is taken to execute the statement statement, and then, in turn, until
all elements are taken. Like for loop statements, statement can be a single statement or a statement block. Code 4-2 can be
in the form of code 4-3, and the result of output is exactly the same as that on the code.
use the foreach loop to get array elements
[code]$ms_office = array (
'word',
'excel',
'outlook'). This code sequentially stores each element of the array in variable $software and prints it out. Foreach loop can not only obtain array elements, but also obtain index
[code]$ms_office = array (
'wd'=.gt;'word',
'ec'=.gt;'excel',
'ol'=.gt). .quot;
.quot;;
echo.Quot;
.quot;
}
?.gt; [/code]
in this code, each index loop assigns an index of the array to the variable / variable, and assigns its corresponding value to the variable.