Detailed explanation of the use of each and list in PHP

*文
Release: 2023-03-19 10:10:01
Original
2098 people have browsed it

This article mainly introduces the usage of each and list in PHP, analyzes the usage skills of each and list functions in the form of examples, and analyzes the skills of each combined with the list function to implement array traversal. Friends in need can refer to it. I hope to be helpful.

1. Usage of each

Let’s look at the API first

array each (array &$array)

This is described in the api: each - returns the current key/value pair in the array and moves the array pointer forward one step

Let's first take a look at what the returned array looks like?

<?php 
$arr = array(&#39;你&#39;,&#39;若&#39;,&#39;安&#39;,&#39;好&#39;,&#39;便&#39;,&#39;是&#39;,&#39;晴&#39;,&#39;天&#39;);
print_r(each($arr));
print_r(each($arr));
echo &#39;<hr />&#39;;
/*
返回
Array
(
  [1] => 你
  [value] => 你
  [0] => 0
  [key] => 0
)
Array
(
  [1] => 若
  [value] => 若
  [0] => 1
  [key] => 1
)
*/
//执行相同的一段代码,从‘你&#39;到‘若&#39;,说明each是会每执行一次,游标向数组尾部移动一步
//0和Key存放的是键
//1和value存放的是值
//因此each满足遍历数组的,得到当前的键和值,以及每执行一次,向尾部移动一步游标
//因此循环数组也可以用each这么写
reset($arr);
for(;$tmp=each($arr);){
  echo $tmp[0],&#39;~&#39;,$tmp[1],&#39;<br />&#39;;
}
/*
返回
0~你
1~若
2~安
3~好
4~便
5~是
6~晴
7~天
*/
?>
Copy after login

2. Usage of list

Let’s first look at what the api says

Like array(), this is not a real function, but is a language structure. list() uses one step to assign values ​​to a set of variables.

Let’s look at an example:

<?php 
list($a,$b)=array(10,20);
echo $a,&#39;~&#39;,$b,&#39;<br />&#39;;
//返回10~20
?>
Copy after login

Yes, you can assign values ​​to a set of variables

Let’s look at another example:

<?php 
list($a,$b,,$c)=array(2=>10,3=>20,4=>30,1=>40);
echo $a,&#39;~&#39;,$b,&#39;~&#39;,$c,&#39;<br />&#39;;
//返回notice~40~20
//执行到$a的时候返回给我一个notice:说数组没有0键
?>
Copy after login

According to the general idea, it should Will return: 10~20~40

Why will this notice~40~20 be returned?

Answer: This involves the operating mechanism of the list. This is how the list is assigned

First of all: ignore the array on the right and look at the variables in the List. From left to right it should be $a = arr[0] $b=arr[1] $c=arr[3]

Then: start assigning values ​​from right to left, the order of assignment is $c=arr[3] $b=arr[1 ] $a=arr[0]

So $c=20 $b = 40 Because there is no arr[0], $a gave a warning

3. Use each Implement array traversal with list

<?php 
$arr = array(&#39;你&#39;,&#39;若&#39;,&#39;安&#39;,&#39;好&#39;,&#39;便&#39;,&#39;是&#39;,&#39;晴&#39;,&#39;天&#39;);
for(;list($k,$v)=each($arr);){
  echo $k,&#39;~&#39;,$v,&#39;<br />&#39;;
}
/*
return:
0~你
1~若
2~安
3~好
4~便
5~是
6~晴
7~天
*/
?>
Copy after login

Related recommendations:

Detailed explanation of the PHP file permission function chmod

Detailed explanation of how PHP implements the Hook mechanism

##Detailed explanation of using PHP to find the longest common substring of two strings

The above is the detailed content of Detailed explanation of the use of each and list in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!