Home Backend Development PHP Tutorial 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

Jul 29, 2016 am 08:53 AM
list

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>
Copy after login

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>
Copy after login

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>
Copy after login

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>
Copy after login
').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 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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1269
29
C# Tutorial
1248
24
How to implement Redis List operation in php How to implement Redis List operation in php May 26, 2023 am 11:51 AM

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

How to convert JSONArray to List in Java How to convert JSONArray to List in Java May 04, 2023 pm 05:25 PM

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

How to convert list to numpy How to convert list to numpy Nov 22, 2023 am 11:29 AM

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.

Why doesn't list.sort() return a sorted list in Python? Why doesn't list.sort() return a sorted list in Python? Sep 18, 2023 am 09:29 AM

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 List.Sort function in C# How to sort a list using List.Sort function in C# Nov 17, 2023 am 10:58 AM

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

How to convert array to List in Java How to convert array to List in Java Apr 19, 2023 am 09:13 AM

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&quot

What are the common methods of List in Java basics What are the common methods of List in Java basics May 14, 2023 am 10:16 AM

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

Comparison of Java Map and other collection frameworks: advantages and disadvantages analysis and application scenario guide Comparison of Java Map and other collection frameworks: advantages and disadvantages analysis and application scenario guide Feb 19, 2024 pm 10:24 PM

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

See all articles