PHP using foreach to iterate over arrays more efficient _php tutorials

Source: Internet
Author: User
There are two ways to use foreach ():

foreach (Array_Name as $value)    {       statement;    }

The array_name here is the name of the array you want to traverse, each time the value of the current element of the Array_Name array is assigned to $value, and the subscript inside the array moves down one step, that is, the next loop returns to the next element.

foreach (array_name as $key = = $value)    {        statement;      

The difference between this and the first method is that there is a $key, that is, in addition to assigning the value of the current element to $value, the key value of the current element is assigned to the variable $key in each loop. The key value can be a subscript value or a string. For example, "0" in Book[0]=1, book[id]= "id" in "001".

Program Examples:

1

 
  '; $a = array (1, 2, 3,); foreach ($a as $v) {   echo "current value of". $a. ":". $v. "
";}? >//Run result foreach example 1:value only current value of $a: 1Current value of $a: 2Current value of $a: 3Current value O F $a: 17

2

/*-------------------------------------------------------------------------*//* foreach example 2:value (with key Printed for illustration) */echo '
'.'
'." foreach example 2:value (with key printed for illustration) "."
'; $a = array (1, 2, 3,); $i = 0; /* For illustrative purposes only */foreach ($a as $v) { echo "" $a [$i] + $v "."
'; $i + +;} Program Run result foreach example 2:value (with key printed for illustration) $a [0] = = 1$a[1] = 2$a[2] + 3$a[3] + 1 7

3

/*-------------------------------------------------------------------------*//* foreach example 3:key and value */ Echo '
'.'
'." foreach Example 3:key and value "."
'; $a = array ( "one" = 1, "one" + = 2, "three" = 3, "Seventeen"); foreach ($a as $k = = $v) { echo "" $a [$k] = $v "."
';} Program Run result foreach example 3:key and value $a [one] = 1$a[two] = 2$a[three] + 3$a[seventeen] + 17

4

/*-------------------------------------------------------------------------*//* foreach Example 4: Multi-dimensional Arrays */echo '
'.'
'." foreach Example 4:multi-dimensional arrays ". '
'; $a = array (); $a [0][0] = "a"; $a [0][1] = "B"; $a [1][0] = "Y"; $a [1][1] = "Z"; foreach ($a as $v 1) { foreach ($v 1 as $v 2) { echo "$v 2" n "; }} Program Run result foreach example 4:multi-dimensional arrays a b y Z

5

/*-------------------------------------------------------------------------*//* foreach Example 5:dynamic arrays */ Echo '
'.'
'." foreach Example 5:dynamic arrays ". '
'; foreach (Array (1, 2, 3, 4, 5) as $v) { echo "$v" n ";} Program Run result foreach example 5:dynamic arrays 1 2 3 4 5

You can also use this:

$messageNav [' home '] =root_path; $messageNav [' talent exchange '] = "#" $messageNav [' dynamic column '] = "hragent/cn/"
 
   $value):?>
 
   ">
 
   >" 
 
   class= "Onlink" >
 
  
 
  
 
  

http://www.bkjia.com/PHPjc/752574.html www.bkjia.com true http://www.bkjia.com/PHPjc/752574.html techarticle foreach () is used in two ways: foreach (Array_Name as $value) {statement;} Here the Array_Name is the name of the array you want to traverse, each time the value of the current element of the Array_Name array is assigned ...

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