PHP Traversal Loop Array implementation method, PHP loop array _php Tutorial

Source: Internet
Author: User
Tags vars

PHP Traversal Loop Array implementation method, PHP loop array


Simply use the foreach for list every while to iterate through the array, including a normal one-dimensional array and a two-dimensional array traversal method, with a detailed description of how each function is used.

 
  
  
  1. Array ('aa ' =>33,' BB ' =>22,' www.phpfensi.com ' =>11),Array (' DD ' =>44, ' ee ' =>55,

Use the for foreach to traverse one-dimensional data:

 
  
  
  1. $i = 0; $i <count ($foreach);
  2. ' foreach['. $i.']= '. $foreach [$i].'

Then look at using foreach to traverse:

 
  
  
  1. ' Foreach= '. $v.'

It's all about traversing one-dimensional data, and here we look at two-dimensional arrays:

 
  
  
  1. foreach ($key =
  2. foreach ($k =
  3. ' $arr ['. $key. '] ['. $k.']= '.
  4. "

Take a look at list each:

The each () function generates an array of the key names and key values of the elements pointed to by the current internal pointer to the array, and moves the internal pointer forward.

The returned array contains four elements: The key is named 0,1,key and value. Unit 0 and key contain the key name of the array cell, and 1 and value contain the data.

If the inner pointer crosses the array range, this function returns FALSE.

syntax:each (array) parameter description

Array required, which specifies the arrays to be used.

List function

The list () function assigns values to a set of variables using the elements in the array.

Note that, like array (), list () is actually a language structure, not a function.

Syntax:list (var1,var2 ...) Parameter description

Var1 required, the first variable that needs to be assigned a value.

VAR2 is optional and can have multiple variables.

Tips and Comments:

Note: This function is used only for arrays of numeric indexes and assumes that the numeric index starts at 0.

 
  
  
  1. Array (' red ',' Blue ',' green ',
  2. while (list ($key,$val) = each (
  3. "Other list of $val.
    ";

For statement:

You can use the FOR statement if you have determined how many times the code block repeats.

Grammar:

 
  
  

Note: The For statement has three parameters, the first parameter initializes the variable, the second parameter holds the condition, the third parameter contains the increment required to execute the loop, if more than one variable is included in the initialization or increment parameter, a comma is required, and the condition must be computed as True or FALSE.

The foreach statement is used to iterate through an array.

For Each loop, the value of the current array element is assigned to the value variable (the array pointer moves one at a time)-and so on.

http://www.bkjia.com/PHPjc/1051884.html www.bkjia.com true http://www.bkjia.com/PHPjc/1051884.html techarticle PHP Traversal Loop Array Implementation method, the PHP loop array simply using the foreach for list every while to iterate through the array, including the normal one-dimensional array and two-dimensional array traversal method, the following detailed ...

  • 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.