PHP each and list usage analysis, phpeachlist usage _php tutorial

Source: Internet
Author: User

Analysis of each and list usage in PHP, phpeachlist usage


This paper analyzes the usage of each and list in PHP. Share to everyone for your reference, 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.

<?php $arr = Array (' You ', ' if ', ' ann ', ' good ', ' then ', ' yes ', ' clear ', ' Day ');p Rint_r (each ($arr));p Rint_r (each ($arr)); Echo '
 
  */* 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:

<?php list ($a, $b) =array (10,20), echo $a, ' ~ ', $b, '
';//Return to 10~20?>

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, ' ~ ', $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

<?php $arr = Array (' You ', ' if ', ' ann ', ' good ', ' then ', ' yes ', ' clear ', ' Day '), ' for (; list ($k, $v) =each ($arr);) {  echo $k, ' ~ ', $v, '
';} /*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.

Articles you may be interested in:

    • PHP 3 ways to iterate through an array list (), each (), and while summary
    • PHP Loop Statement Note (foreach,list)
    • PHP Array Traversal method Daquan (Foreach,list,each)
    • A brief analysis of the use of list () function in PHP
    • In-depth explanation of the PHP list () function

http://www.bkjia.com/PHPjc/1089952.html www.bkjia.com true http://www.bkjia.com/PHPjc/1089952.html techarticle php each and list usage analysis, phpeachlist usage This article analyzes each and list usage in PHP. Share to everyone for reference, as follows: 1.each usage first look at API array ea ...

  • Related Article

    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.