Comparison of each and list usage in PHP

Source: Internet
Author: User
PHP each and list usage, combined with instance form analysis of each and list functions of the use of skills, and examples of each combined with the list function to implement the array traversal skills

Specific as follows:

Usage of 1.each

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 take a look at what the returned array looks like.

 
  */* Returns an array (  [1] = = You  [value] = you  [0] = 0  [key] + 0) Array (  [1] = if  [value] =&G T If  [0] = 1  [key] + 1) *///executes the same piece of code, from ' You ' to ' if ', stating that each is executed every time, the cursor moves to the end of the array//0 and key is stored by the key//1 and value is the value// So each satisfies the traversal of the array, gets the current key and value, and each time it executes, one-step cursor is moved to the tail///So the loop array can also be written with each reset ($arr), the for (; $tmp =each ($arr);) {  echo $tmp [0 ], ' ~ ', $tmp [1], '
';} /* Return 0~ you 1~ if 2~ ann 3~ good 4~ then 5~ is 6~ sunny 7~ days */?>

Usage of 2.list

Let's see what the API says.

Like Array (), this is not a real function, but a language structure. List () assigns a set of variables in one-step operation.

Take a look at an example:

 
  ';//Return to 10~20?>

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

Let's look at another example:

 
  10,3=>20,4=>30,1=>40), echo $a, ' ~ ', $b, ' ~ ', $c, '
';//Return notice~40~20//execute to $ A when returning to me a notice: said array does not have 0 keys?>

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

Why did you return to this notice~40~20?

A: This involves the operation of the list, the list is so assigned

First: Do not control the array on the right, see the list of variables, from left to right should be $a = arr[0] $b =arr[1] $c =arr[3]

Then: Assign values from right to left, order of assignment $c =arr[3] $b =arr[1] $a =arr[0]

So $c=20 $b = 40 because there is no arr[0], so $ A has given a warning

3. Iterating through arrays with each and list

 
  ';} /*return:0~ you 1~ if 2~ ann 3~ good 4~ then 5~ is 6~ sunny 7~ days */?>

I hope this article is helpful to you in PHP programming.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.