• 5_PHP数组_3_数组处理函数及其应用_3_数组指针函数


    以下为学习孔祥盛主编的《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
  • 相关阅读:
    Maven下java.lang.NoClassDefFoundError
    FastDFS的配置、部署与API使用解读(5)FastDFS配置详解之Tracker配置(转)
    FastDFS的配置、部署与API使用解读(4)FastDFS配置详解之Client配置(转)
    FastDFS的配置、部署与API使用解读(3)以流的方式上传文件的客户端代码(转)
    FastDFS的配置、部署与API使用解读(2)以字节方式上传文件的客户端代码(转)
    FastDFS的配置、部署与API使用解读(1)Get Started with FastDFS(转)
    软件测试全景图(转)
    让Quality Center走下神坛--测试管理工具大PK(转)
    测试管理杂谈之理论篇(转)
    管理杂谈之理论篇(转)
  • 原文地址:https://www.cnblogs.com/xiaoxuStudy/p/11823942.html
Copyright © 2020-2023  润新知