


PHP--Usage of each and list list for each safe each list as value i js traverse list collection eac
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
So
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>
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.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











List operation //Insert a value from the head of the list. $ret=$redis->lPush('city','guangzhou');//Insert a value from the end of the list. $ret=$redis->rPush('city','guangzhou');//Get the elements in the specified range of the list. 0 represents the first element of the list, -1 represents the last element, and -2 represents the penultimate element. $ret=$redis->l

1: JSONArray to ListJSONArray string to List//Initialize JSONArrayJSONArrayarray=newJSONArray();array.add(0,"a");array.add(1,"b");array.add(2,"c") ;Listlist=JSONObject.parseArray(array.toJSONString(),String.class);System.out.println(list.to

Method to convert list to numpy: 1. Use the numpy.array() function. The first parameter of the function is a list object, which can be a one-dimensional or multi-dimensional list; 2. Use the numpy.asarray() function, which will try its best to Use the data type of the input list; 3. Use the numpy.reshape() function to convert the one-dimensional list into a multi-dimensional NumPy array; 4. Use the numpy.fromiter() function, the first parameter of the function is an iterable object.

Example In this example, we first look at the usage of list.sort() before continuing. Here, we have created a list and sorted it in ascending order using sort() method - #CreatingaListmyList=["Jacob","Harry","Mark","Anthony"]#DisplayingtheListprint("List=",myList)#SorttheListsinAscendingOrdermyList .sort(

How to sort a list using the List.Sort function in C# In the C# programming language, we often need to sort the list. The Sort function of the List class is a powerful tool designed for this purpose. This article will introduce how to use the List.Sort function in C# to sort a list, and provide specific code examples to help readers better understand and apply this function. The List.Sort function is a member function of the List class, used to sort elements in the list. This function receives

1. The most common way (not necessarily the best) is through Arrays.asList(strArray). After converting the array into List, you cannot add or delete the List, you can only check and modify it, otherwise an exception will be thrown. Key code: Listlist=Arrays.asList(strArray);privatevoidtestArrayCastToListError(){String[]strArray=newString[2];Listlist=Arrays.asList(strArray);//Insert a piece of data into the converted list list.add(" 1"

1. Introduction to List interface List is an ordered collection and a repeatable collection. It inherits the Collection interface. Repeated elements can appear in the List collection, and the element at the specified position can be accessed through the index (subscript). 2. List common methods - voidadd (intindex, Obejctelement) method 1. The voidadd (intindex, Obejctelement) method inserts the element element at the specified position and moves the subsequent element back one element. 2.voidadd(intindex,Obejctelemen

1. Overview of the Map Collection Framework The Map collection framework is a key-value pair data structure that allows you to use keys to find and store values. Each key in the Map is unique and can only be associated with one value. Common implementations in the Map collection framework include HashMap, TreeMap and LinkedHashMap. 1.HashMapHashMap is the most widely used Map implementation in Java. It stores data based on hash tables. HashMap has excellent performance, and the time complexity of search and insertion operations is O(1), but it does not guarantee the order of elements. Demo code: Mapmap=newHashMap
