The following is learning Kong Xiangsheng editor of "PHP Programming Fundamentals tutorial and example" (second edition) notes made.
Function array pointer
1. key () function
program:
. 1 ? < PHP 2 $ Interests [2] = "Music" ; . 3 $ Interests [. 5] = "Movie" ; . 4 $ Interests [. 1] = "Computer" ; . 5 $ Interests [] = "Software" ; . 6 var_dump ( key ( $ Interests )); // int 2 // returns an array of arr "current pointer" within the meaning of the keys elements. 7 ?>
Output:
D: \ wampServer \ www \ Apache Server home \ practice doing \ routine .php: 6: int 2
2. current () function
program:
. 1 ? < PHP 2 $ Interests [2] = "Music" ; . 3 $ Interests [. 5] = "Movie" ; . 4 $ Interests [. 1] = "Computer" ; . 5 $ Interests [] = "Software" ; . 6 var_dump ( current ( $ Interests )); // String 'Music' (length =. 5) // returns an array of arr "current pointer" refer to "value" elements 7 >?
Output:
D: \ wampServer \ www \ Apache Server home \ practice doing \ routine .php:. 6: String 'Music' (length =. 5)
3. next () function
program:
1 <?php 2 $interests[2] = "music"; 3 $interests[5] = "movie"; 4 $interests[1] = "computer"; 5 $interests[] = "software"; 6 $second = next($interests); 7 $third = next($interests); 8 var_dump(key($interests)); //int 1 9 echo "<br/>"; 10 var_dump(current($interests)); //string 'computer' (length=8) 11 echo "<br/>"; 12 var_dump($second); //string 'movie' (length=5) 13 echo "<br/>"; 14 var_dump($third); //string 'computer' (length=8) 15 ?>
Output:
D: \ wampServer \ www \ Apache Server home \ practice doing \ routine .php:. 8: int. 1 D : \ WampServer \ WWW \ the Apache server main directory \ practice doing \ routine .php: 10: String 'Computer' (length . 8 = ) D : \ WampServer \ WWW \ the Apache server main directory \ practice doing \ routine .php: 12 is: String 'Movie' (length =. 5 ) D : \ WampServer \ WWW \ the Apache server main directory \ practice doing \ routine. PHP: 14: String 'Computer' (length =. 8)
4. end () function
program:
1 <?php 2 $interests[2] = "music"; 3 $interests[5] = "movie"; 4 $interests[1] = "computer"; 5 $interests[] = "software"; 6 $end = end($interests); 7 var_dump(key($interests)); //int 6 8 echo "<br/>"; 9 var_dump(current($interests)); //string 'software' (length=8) 10 echo "<br/>"; 11 var_dump($end); //string 'software' (length=8) 12 ?>
Output:
D: \ wampServer \ www \ Apache Server home \ practice doing \ routine .php:. 7: int. 6 D : \ WampServer \ WWW \ the Apache server main directory \ practice doing \ routine .php:. 9: String 'Software' (length . 8 = ) D : \ WampServer \ WWW \ the Apache server main directory \ practice doing \ routine .php:. 11: String 'Software' (length =. 8)
5. prev () function
program:
1 <?php 2 $interests[2] = "music"; 3 $interests[5] = "movie"; 4 $interests[1] = "computer"; 5 $interests[] = "software"; 6 $end = end($interests); 7 $prev = prev($interests); 8 var_dump(key($interests)); //int 1 9 echo "<br/>"; 10 var_dump(current($interests)); //string 'computer' (length=8) 11 echo "<br/>"; 12 var_dump($end); //string 'software' (length=8) 13 echo "<br/>"; 14 var_dump($prev); //string 'computer' (length=8) 15 ?>
Output:
D: \ wampServer \ www \ Apache Server home \ practice doing \ routine .php:. 8: int. 1 D : \ WampServer \ WWW \ the Apache server main directory \ practice doing \ routine .php: 10: String 'Computer' (length . 8 = ) D : \ WampServer \ WWW \ the Apache server main directory \ practice doing \ routine .php: 12 is: String 'Software' (length. 8 = ) D : \ WampServer \ WWW \ the Apache server main directory \ practice doing \ routine. PHP: 14: String 'Computer' (length =. 8)
6. reset () function
program:
1 <?php 2 $interests[2] = "music"; 3 $interests[5] = "movie"; 4 $interests[1] = "computer"; 5 $interests[] = "software"; 6 $end = end($interests); 7 $first = reset($interests); 8 var_dump(key($interests)); //int 2 9 echo "<br/>"; 10 var_dump(current($interests)); //string 'music' (length=5) 11 echo "<br/>"; 12 var_dump($end); //string 'software' (length=8) 13 echo "<br/>"; 14 var_dump($first); //string 'music' (length=5) 15 ?>
Output:
D: \ wampServer \ www \ Apache Server home \ practice doing \ routine .php:. 8: 2 int D : \ WampServer \ WWW \ the Apache server main directory \ practice doing \ routine .php: 10: String 'Music' (length . 5 = ) D : \ WampServer \ WWW \ the Apache server main directory \ practice doing \ routine .php: 12 is: String 'Software' (length =. 8 ) D : \ WampServer \ WWW \ the Apache server main directory \ practice doing \ routine. PHP: 14: String 'Music' (length =. 5)
7. each () function
program:
1 <?php 2 $interests[2] = "music"; 3 $interests[5] = "movie"; 4 $interests[1] = "computer"; 5 $interests[] = "software"; 6 $each = each($interests); 7 print_r( $each ); //Array ( [1] => music [value] => music [0] => 2 [key] => 2 ) 8 echo "<br/>"; 9 echo current($interests); //movie 10 ?>
Output:
Description:
PHP 7.2 abandoned each () method.
Iterate
Use list () language structure, each () function and loops can be achieved
program:
1 <?php 2 $fruits = array( 'orange', 'apple', 'banana'); 3 $colors = array( 'orange', 'red', 'yellow' ); 4 $temp = array_combine( $fruits,$colors ); 5 do{ 6 $key = key($temp); 7 $value = current($temp); 8 echo $key." => ".$value."<br/>"; 9 }while( next($temp) ); 10 ?>
输出:
orange => orange apple => red banana => yellow