PHP array _5_3_ array processing functions and their application _5_ array traversal language structure

The following is learning Kong Xiangsheng editor of "PHP Programming Fundamentals tutorial and example" (second edition) notes made.

 

 

Array traversal language structure

1. foreach ( array as $value )

program:

1 <?php
2 $interests[2] = "music";
3 $interests[5] = "movie";
4 $interests[1] = "computer";
5 $interests[] = "software";
6 foreach ( $interests as $value) {
7     echo $value."<br/>";
8 }
9 ?>

Output:

music
movie
computer
software

 

2. foreach( array as $key=>$value )

program:

1 <?php
2 $interests[2] = "music";
3 $interests[5] = "movie";
4 $interests[1] = "computer";
5 $interests[] = "software";
6 foreach ( $interests as $key=>$value) {
7     echo $key."=>".$value."<br/>";
8 }
9 ?>

Output:

2=>music
5=>movie
1=>computer
6=>software

 

foreach language structure can be achieved in addition to traversal of the array, may also modify the values of the key array element or implemented .

The following procedure 1, program 2, program 3, program only 1 array element to achieve the modified value.

Program 1:

 1 <?php
 2 $interests[2] = "music";
 3 $interests[5] = "movie";
 4 $interests[1] = "computer";
 5 $interests[] = "software";
 6 foreach ( $interests as $key=>&$value) {    //注意这里的 &$value
 7     $value = "I like ".$value;
 8 }
 9 print_r($interests);
10 //Array ( [2] => I like music [5] => I like movie [1] => I like computer [6] => I like software )
11 ?>

Output:

Array ( [2] => I like music [5] => I like movie [1] => I like computer [6] => I like software )

 

Program 2:

 1 <?php
 2 $interests[2] = "music";
 3 $interests[5] = "movie";
 4 $interests[1] = "computer";
 5 $interests[] = "software";
 6 foreach ( $interests as $key=>$value) {
 7     $interests[$key] = "I like ".$value;
 8 }
 9 print_r($interests);
10 ?>

Output:

Array ( [2] => I like music [5] => I like movie [1] => I like computer [6] => I like software )

 

Procedure 3:

 1 <?php
 2 $interests[2] = "music";
 3 $interests[5] = "movie";
 4 $interests[1] = "computer";
 5 $interests[] = "software";
 6 foreach ( $interests as $key=>$value) {
 7     $value = "I like ".$value;
 8 }
 9 print_r($interests);
10 ?>

Output:

Array ( [2] => music [5] => movie [1] => computer [6] => software )

 

foreach language structure is a copy of the operation of the array, rather than the array itself. During the foreach loop through the array, and try not to use an array of function pointers Operation "current pointer" (Current) , otherwise it will backfire.

program:

 1 <?php
 2 $interests[2] = "music";
 3 $interests[5] = "movie";
 4 $interests[1] = "computer";
 5 $interests[] = "software";
 6 foreach ( $interests as $key=>$value) {
 7     echo "I like ".current($interests)."!<br/>";
 8     echo $value."<br/>";
 9 }
10 print_r($interests);
11 ?>

Output:

I like music!
music
I like music!
movie
I like music!
computer
I like music!
software
Array ( [2] => music [5] => movie [1] => computer [6] => software )

 

Guess you like

Origin www.cnblogs.com/xiaoxuStudy/p/11826765.html