Traversing all elements in an array is a common operation, and functions such as queries can be completed during the traversal process. In our daily life, if we want to go to the mall to buy a piece of clothing, we need to walk around the mall to see if we want the clothes we want again. The process of shopping in the mall is equivalent to the operation of traversing an array. There are many ways to traverse arrays in PHP. Here we will introduce you to the two most commonly used methods.
The first one: use the foreach structure to traverse the array
For an array variable $url that contains a large number of URLs, if you use the echo statement to Outputting one by one will be quite tedious, but by traversing the array through the foreach structure, you can easily obtain the data information. The sample code is as follows:
<?php header("Content-Type:text/html; charset=utf-8"); $url = array( "PHP中文网"=>"www.php.cn", "百度" => "www.baidu.com", "搜狗"=>"www.sogou.com", ); //声明数组 foreach($url as $link){ echo $link.'<br>'; } ?>
The traversal result is:
In the above code, PHP executes the loop body (echo statement) once for each element of the array $URL, and assigns $link to the current array element. Each element is processed in the internal order of the array.
Second type: The list() function traverses the array
The list() function assigns the values in the array to some variables. Similar to the array() function, the list() function is not a real function, but a language construct. The list() function can only be used for numerically indexed arrays with indexes starting from 0.
The syntax format is as follows:
void list(mixed ...)
The parameter mixed is the name of the variable to be assigned a value.
The following will explain the comprehensive application of the list() function and each() function through specific examples to obtain the user login information stored in the group number.
The specific development steps are as follows:
1. Use development tools to create a new PHP dynamic page and save it as index.php.
2. Use HTML markup to design the page. First create a user login form to enter user login information, then use the each() function to extract the contents of the global array $_POST, and finally use the white statement to loop and output the attention information submitted by the user.
The sample code is as follows:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <!---------------------------------------------定义用户登录表单信息----------------------------------------------------> <form name="form1" method="post"> <table width="323" border="1" cellpadding="1" cellspacing="1" bordercolor="#66cc33" bgcolor="#FFFFFF"> <tr> <td width="118" height="24" bgcolor="#CCFF33">用户名</td> <td width="192" height="24" bgcolor="#CCFF33"> <input type="text" name="user" id="user" size="24"> </td> </tr> <tr> <td height="24" bgcolor="#CCFF33">密 码</td> <td height="24" bgcolor="#CCFF33"> <input type="password" name="pwd" id="pwd" size="24"> </td> </tr> <tr bgcolor="#CCFF33"> <td height="24" colspan="2"> <input type="submit" name="submit" value="登录"> </td> </tr> </table> </form> </body> </html> <?php //输出用户登录信息 while(list($name,$value)=each($_POST)){ if($name!="submit"){ echo "$name=$value<br>"; } } ?>
3. Enter the address in the browser, press the Enter key, enter the user name and password, click the "Login" button, and the running result is as shown below :
Description:
each() function is used to return the array value of the current pointer position, and at the same time change the pointer Advance to the next location. The returned array contains 4 keys, keys 0 and key contain the key names, and keys 1 and value contain the corresponding data. If the pointer is already at the end of the array when the program executes the each() function, false is returned.
【Related tutorial recommendations】
1. Relevant topic recommendations: "php array (Array) 》
2. Recommended related video courses:
《Using for loop to traverse arrays: index and associative array 》
《Use while loop to traverse arrays: index and associative array》
《Use foreach loop To traverse: index and associative array》
The above is the detailed content of How to loop output of an array in php? Introduction to the method of traversing the array. For more information, please follow other related articles on the PHP Chinese website!