当前位置:网站首页>Summary of three methods of PHP looping through arrays list (), each (), and while
Summary of three methods of PHP looping through arrays list (), each (), and while
2022-07-03 16:35:00 【Leaf drop traceless 123】
while (list($key, $value) = each($list)) {} The performance of writing is higher than foreach,
turn :http://www.jb51.net/article/57560.htm
①each() function
each() The function needs to pass an array as an argument , Returns the key of the current element in the array / It's worth it , And move the array pointer backward to the next element . key / Value pairs are returned with 4 A mixed array of associations and indexes of elements , The key names are 0、1、key and value. Where key name 0 and key The corresponding value is the same , Is the key name of the array element ,1 and value Contains the values of array elements . If the internal pointer goes past the end of the array , be each() return FALSE.each() The function is used as follows :
Copy code The code is as follows :
< ?php
$contact = array("ID" => 1," full name " => " Gao Mou "," company " => "A company "," Address " => " The Beijing municipal ",);
$id = each($contact); // Returns an array of $contact The key of the first element in / It's worth it , It's with 4 Array of elements
print_r($id); // The output array $id:Array([1]=>1,[value]=>1,[0]=>ID,[key]=>ID)
$name = each($contact); // Returns an array of $contact The key of the second element in / It's worth it , It's with 4 Array of elements
print_r($name); // Output Array([1]=> Gao Mou ,[value]=> Gao Mou ,[0]=> full name ,[key]=> full name )
$company = each($contact);
print_r($company); // Output Array([1]=>A company ,[value]=>A company ,[0]=> company ,[key]=> company )
$address = each($contact);
print_r($address); // Output Array([1]=> The Beijing municipal ,[value]=> The Beijing municipal ,[0]=> Address ,[key]=> Address )
$no = each($contact);
var_dump($no); // Output bool(false)
?>
②list() function
This is not really a function , It is PHP The structure of language .list() Assign values to a set of variables in one step , That is, assign the values in the array to some variables .list() Can only be used for arrays with numeric indexes and assume that the array index is from 0 Start . The syntax format is as follows :
Copy code The code is as follows :
list(mixed varname,mixed…) = array_expression
list() Statements are very different from other functions in use , Instead of receiving an array directly as a parameter . But through “=” Operator in the way of assignment , The value of each element in the array , Corresponding assignment list() Every parameter in the function .list() Function converts each parameter in it into a variable that can be used directly in the script . Use as follows :
Copy code The code is as follows :
< ?php
$info = array('coffee','brown','caffeine');
list($drink,$color,$power) = $info;
list($drink,,$power) = $info; // The value of the variable is the first and third value in the array
list( , ,$power) = $info; // The variable value is the value of the third element in the array
?>
Learn from the above example list() After the usage of the function , take each() Functions and list() Functions are used together . The code is as follows :
Copy code The code is as follows :
< ?php
$contact = array("ID" => 1," full name " => " Gao Mou "," company " => "A company "," Address " => " The Beijing municipal ",);
list($key,$value) = each($contact);
echo "$key => $value"; // Output variables $key and $value, In the middle “=>” Division
?>
③while loop Traversal array
Previously introduced each() and list() Use of statements , It's not difficult to understand if you use while Loop through the array . The syntax format used is as follows :
Copy code The code is as follows :
while(list($key,$value) = each(array_expression)){
The loop body
}
This union format traverses a given array_expression Array . stay while() Statement in each loop ,each() Statement sets the key of the current array element , Assign to list() The first parameter variable of the function $key. And the value in the current array element , Assign to list() The second parameter variable in the function $value, also each() After the statement is executed, the pointer inside the array will be moved back one step , So next time while() Statement loop , You will get the key of the next element in the array / It's worth it . Until the end of the array each() Statement returns FALSE,while() Statement stops the loop , End the traversal of the array .
Copy code The code is as follows :
< ?php
$contact = array(
"ID" => 1,
" full name " => " Gao Mou ",
" company " => "A company ",
" Address " => " The Beijing municipal ",
" Telephone " => "(010)98765432",
"EMAIL" => "[email protected]",
);
// With HTML List the information of each element in the array
echo '<dl> A contact information :';
while(list($key,$value) = each($contact)){
echo "<dd>$key : $value</dd>";
}
echo '</dl>';
?>
It can also be yo on Nested traversal of multidimensional arrays in the same way . although while The result of traversing the array and freach Same statement , But these two methods are different . In the use of while Statement after traversing the array ,each() Statement has pointed the internal pointer of the passed in array parameter to the end of the array . When used again while Statement when traversing the same array , The array pointer is already at the end of the array ,each() Statement returns directly FALSE,while Statement will not be executed in a loop . Only in while Call before execution of statement reset() function , Reassign the array pointer to the first element . and foreach Statement will automatically reset the pointer position of the array , When foreach At the beginning of execution , The pointer inside the array will automatically point to the first cell . This means that there is no need to foreach Loop prior call reset() function .
边栏推荐
- 一台服务器最大并发 tcp 连接数多少?65535?
- Netease UI automation test exploration: airtest+poco
- Client does not support authentication protocol requested by server; consider upgrading MySQL client
- Is it safe to open an account with tongdaxin?
- 利用MySQL中的乐观锁和悲观锁实现分布式锁
- 在ntpdate同步时间的时候出现“the NTP socket is in use, exiting”
- Cocos Creator 2.x 自动打包(构建 + 编译)
- arduino-esp32:LVGL项目(一)整体框架
- NSQ source code installation and operation process
- EditText request focus - EditText request focus
猜你喜欢

Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (II)

Explore Netease's large-scale automated testing solutions see here see here

深入理解 SQL 中的 Grouping Sets 语句

NLP四范式:范式一:非神经网络时代的完全监督学习(特征工程);范式二:基于神经网络的完全监督学习(架构工程);范式三:预训练,精调范式(目标工程);范式四:预训练,提示,预测范式(Prompt工程)

MySQL single table field duplicate data takes the latest SQL statement
![[combinatorics] non descending path problem (outline of non descending path problem | basic model of non descending path problem | non descending path problem expansion model 1 non origin starting poi](/img/81/59ed6bebf5d85e9eb71bd4ca261309.jpg)
[combinatorics] non descending path problem (outline of non descending path problem | basic model of non descending path problem | non descending path problem expansion model 1 non origin starting poi

There are several APIs of airtest and poco that are easy to use wrong in "super". See if you have encountered them

跟我学企业级flutter项目:简化框架demo参考

Colab works with Google cloud disk

(Supplement) double pointer topic
随机推荐
Nifi from introduction to practice (nanny level tutorial) - flow
Mysql 单表字段重复数据取最新一条sql语句
Advanced Mathematics (Seventh Edition) Tongji University exercises 2-1 personal solutions
MySQL converts comma separated attribute field data from column to row
Characteristic polynomial and constant coefficient homogeneous linear recurrence
Hibernate的缓存机制/会话级缓存机制
Threejs Part 2: vertex concept, geometry structure
Deep understanding of grouping sets statements in SQL
(补)双指针专题
Multithread 02 thread join
Register in PHP_ Globals parameter settings
探索Cassandra的去中心化分布式架构
高等数学(第七版)同济大学 习题2-1 个人解答
Two sides of the evening: tell me about the bloom filter and cuckoo filter? Application scenario? I'm confused..
Interviewer: how does the JVM allocate and recycle off heap memory
Initial test of scikit learn Library
Le zèbre a été identifié comme un chien, et la cause de l'erreur d'AI a été trouvée par Stanford
【剑指 Offer 】64. 求1+2+…+n
Mysql 将逗号隔开的属性字段数据由列转行
跟我学企业级flutter项目:简化框架demo参考