Home  >  Article  >  Backend Development  >  PHP--Usage of each and list list for each safe each list as value i js traverse list collection eac

PHP--Usage of each and list list for each safe each list as value i js traverse list collection eac

WBOY
WBOYOriginal
2016-07-29 08:53:181889browse

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?

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

2.list usage
Let’s first see what the API says
Like array(), this is not a real function, but a language construct. list() assigns values ​​to a set of variables in one step. ​
Let’s look at an example:

<code><span><?php</span><span>list</span>(<span>$a</span>,<span>$b</span>)=<span>array</span>(<span>10</span>,<span>20</span>);
<span>echo</span><span>$a</span>,<span>'~'</span>,<span>$b</span>,<span>'<br />'</span>;
<span>//返回10~20</span><span>?></span></code>

Yes, you can assign values ​​to a set of variables
Let’s look at another example:

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

According to the general idea, it should return: 10~20~40
Why is this notice~40~20 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 ] ranafter: from right to LeftOpenStart EndowmentValue ,Endowed worth ShunPreface is c=arr[3] b= arr[1 ]a=arr[0]
So c=20b = 40 Because there is no arr[0], $a gave a warning
3. Use each and list to implement array traversal

<code><span><?php</span><span>$arr</span> = <span>array</span>(<span>'你'</span>,<span>'若'</span>,<span>'安'</span>,<span>'好'</span>,<span>'便'</span>,<span>'是'</span>,<span>'晴'</span>,<span>'天'</span>);
<span>for</span>(;<span>list</span>(<span>$k</span>,<span>$v</span>)=each(<span>$arr</span>);){
    <span>echo</span><span>$k</span>,<span>'~'</span>,<span>$v</span>,<span>'<br />'</span>;
}
<span>/*
return:
0~你
1~若
2~安
3~好
4~便
5~是
6~晴
7~天
*/</span><span>?></span></code>
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

The above introduces the usage of PHP--each and list, including the content of each and list. I hope it will be helpful to friends who are interested in PHP tutorials.

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