Abstract: There are two common methods to traverse arrays. one is for loop statement traversal and the other is foreach statement traversal... 1. Traverse arrays.
There are two common methods to traverse arrays. one is for loop statement traversal and the other is foreach statement traversal:
For loop statement traversal: for loop statements can only traverse enumerated arrays whose subscript is continuous.
Foreach statement traversal: foreach is a statement designed specifically to traverse arrays. foreach does not need to consider whether the subscript of an array is a continuous number. as long as it is a PHP array, foreach can be used for traversal. Code demonstration:
$ Meiju_arr = array ("pig", "www.wozhutou.com", "E-Commerce", "male", "Web engineer ");
// For loop statement traversal
For ($ I = 0; $ I <count ($ meiju_arr); $ I ++ ){
Echo "number". $ I. "element value:". $ meiju_arr [$ I]; // output the value of the array element indexed to I
} Here, the count () function is used to calculate the number of array elements in the array.
// The foreach statement traverses the array as a value
Foreach ($ meiju_arr as $ value ){
Echo $ value;
}
// The foreach statement traverses the array as a key-value pair.
Foreach ($ meiju_arr as $ key => $ value ){
Value of the element whose echo "index is". $ key. ":". $ value;
}
2. system pre-defined array
These functions are very powerful. Must be used
$ _ 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, which records information related to the system running PHP, such as the system name and system path.
$ _ 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 all input variables, including $ _ GET, $ _ POST, and $ _ COOKIE.
$ _ FILES: an array consisting of uploaded File items passed by the HTTP post method
$ _ COOKIE: an array composed of variables passed by HTTP Cookies.
$ _ SESSION: an array containing SESSION variables in the current script
This is the end of today.
The above is the basic content of PHP array application (4). For more information, please follow the PHP Chinese network (www.php1.cn )!