以下为学习孔祥盛主编的《PHP编程基础与实例教程》(第二版)所做的笔记。
数组指针函数
1. key() 函数
程序:
1 <?php 2 $interests[2] = "music"; 3 $interests[5] = "movie"; 4 $interests[1] = "computer"; 5 $interests[] = "software"; 6 var_dump(key($interests)); //int 2 //返回数组 arr 中"当前指针"所指元素的键名。 7 ?>
输出:
D:wampServerwwwApache服务器主目录practise例程.php:6:int 2
2. current() 函数
程序:
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) //返回数组 arr 中“当前指针”所指元素的“值” 7 ?>
输出:
D:wampServerwwwApache服务器主目录practise例程.php:6:string 'music' (length=5)
3. next() 函数
程序:
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 ?>
输出:
D:wampServerwwwApache服务器主目录practise例程.php:8:int 1 D:wampServerwwwApache服务器主目录practise例程.php:10:string 'computer' (length=8) D:wampServerwwwApache服务器主目录practise例程.php:12:string 'movie' (length=5) D:wampServerwwwApache服务器主目录practise例程.php:14:string 'computer' (length=8)
4. end() 函数
程序:
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 ?>
输出:
D:wampServerwwwApache服务器主目录practise例程.php:7:int 6 D:wampServerwwwApache服务器主目录practise例程.php:9:string 'software' (length=8) D:wampServerwwwApache服务器主目录practise例程.php:11:string 'software' (length=8)
5. prev() 函数
程序:
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 ?>
输出:
D:wampServerwwwApache服务器主目录practise例程.php:8:int 1 D:wampServerwwwApache服务器主目录practise例程.php:10:string 'computer' (length=8) D:wampServerwwwApache服务器主目录practise例程.php:12:string 'software' (length=8) D:wampServerwwwApache服务器主目录practise例程.php:14:string 'computer' (length=8)
6. reset() 函数
程序:
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 ?>
输出:
D:wampServerwwwApache服务器主目录practise例程.php:8:int 2 D:wampServerwwwApache服务器主目录practise例程.php:10:string 'music' (length=5) D:wampServerwwwApache服务器主目录practise例程.php:12:string 'software' (length=8) D:wampServerwwwApache服务器主目录practise例程.php:14:string 'music' (length=5)
7. each() 函数
程序:
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 ?>
输出:
说明:
PHP 7.2 废弃了each()方法。
数组的遍历
使用list()语言结构、each()函数和循环语句可以实现
程序:
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