Home >Backend Development >PHP Tutorial >Basics of array application in PHP (4)

Basics of array application in PHP (4)

黄舟
黄舟Original
2016-12-22 10:38:531091browse

1. Traverse arrays

There are two commonly used methods of traversing arrays, one is for loop statement traversal, and the other is foreach statement traversal:

for loop statement traversal: for loop statement can only traverse subscripts that are consecutive Enumeration array.

foreach statement traversal: foreach is a statement specially designed for traversing arrays. Foreach does not need to consider whether the subscripts of the array are consecutive numbers. As long as it is a PHP array, foreach traversal can be used. Code demonstration:

$meiju_arr = array("Pig Head","www.wozhutou.com","E-commerce","Male","Web Engineer");
//for loop statement traversal
for($i= 0 ; $i < count($meiju_arr ) ; $i++){
echo "The value of the "th" . $i . "element:" . $meiju_arr [$i] ; //Output the value of the array element with index i Value
}Here, the function of count() function is to count the number of array elements in the array
//The foreach statement traverses the array in the form of value
foreach($meiju_arr as $value){
echo $value;
}

//The foreach statement traverses the array in the form of key-value pairs
foreach($meiju_arr as $key => $value){
echo "The value of the element with index ".$key.":" .$value;
}
2. System predefined array

These functions are very powerful. Be sure to use

$_SERVER: Server variable array, an array created by the web server. Its content includes header information, path, script location and other information

$_ENV: Environment variable array, recording information related to the system PHP is running on , such as system name, system path, etc.

$_GET: An array composed of variables passed by the HTTP get method

$_POST: An array composed of variables passed by the HTTP post method

$_REQUEST: An array of variables entered by all users, Including input content contained in $_GET, $_POST, and $_COOKIE

$_FILES: an array composed of uploaded file items passed by the HTTP post method

$_COOKIE: an array composed of variables passed by HTTP Cookies.

$_SESSION: is an array containing session variables in the current script

That’s it for today.

The above is the content of PHP’s Array Application Basics (4). For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
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