分享

PHP数组_5_3_数组处理函数及其应用_6_数组检索函数

 印度阿三17 2019-11-10

以下为学习孔祥盛主编的《PHP编程基础与实例教程》(第二版)所做的笔记。

 

数组检索函数

1. array_keys() 函数

程序:

 1 <?php
 2 $interests[2] = "music";
 3 $interests[5] = "movie";
 4 $interests[1] = "computer";
 5 $interests[] = "software";
 6 $interests[] = "computer";
 7 $keys = array_keys($interests);
 8 print_r($keys); //Array ( [0] => 2 [1] => 5 [2] => 1 [3] => 6 [4] => 7 )
 9 echo "<br/>";
10 $searchKeys1 = array_keys($interests,"computer");
11 print_r($searchKeys1);  //Array ( [0] => 1 [1] => 7 )
12 echo "<br/>";
13 $searchKeys2 = array_keys($interests,"Computer");
14 print_r($searchKeys2);  //Array ( )
15 //如果 searchValue 是字符串, 比较时区分大小写。
16 ?>

输出:

Array ( [0] => 2 [1] => 5 [2] => 1 [3] => 6 [4] => 7 ) 
Array ( [0] => 1 [1] => 7 ) 
Array ( )

 

2. array_values() 函数

程序:

1 <?php
2 $interests[2] = "music";
3 $interests[5] = "movie";
4 $interests[1] = "computer";
5 $interests[] = "software";
6 $interests[] = "computer";
7 $values = array_values($interests);
8 print_r( $values );
9 ?>

输出:

Array ( [0] => music [1] => movie [2] => computer [3] => software [4] => computer )

 

3. in_array() 函数

程序:

 1 <?php
 2 $words = array("JAVA","PHP",".NET");
 3 $javaExisted = in_array("JAVA",$words);
 4 $phpExisted = in_array("PHP",$words);
 5 var_dump($javaExisted);     //boolean true
 6 echo "<br/>";
 7 var_dump($phpExisted);      //boolean true
 8 echo "<br/>";
 9 
10 $numbers = array('1.10',12.4,1.13);
11 $numExisted1 = in_array(1.10,$numbers);
12 $numExisted2 = in_array(1.10,$numbers,TRUE);  //会比较数据类型是否相同
13 var_dump($numExisted1);     //boolean true
14 echo "<br/>";
15 var_dump($numExisted2);     //boolean false
16 ?>

输出:

D:\wampServer\www\Apache服务器主目录\practise\例程.php:5:boolean true

D:\wampServer\www\Apache服务器主目录\practise\例程.php:7:boolean true

D:\wampServer\www\Apache服务器主目录\practise\例程.php:13:boolean true

D:\wampServer\www\Apache服务器主目录\practise\例程.php:15:boolean false

 

4. array_key_exists() 函数

程序:

1 <?php
2 $words = array( "SUN"=>"JAVA","Microsoft"=>".NET" );
3 $keyExisted1 = array_key_exists("SUN",$words);
4 $keyExisted2 = array_key_exists("sun",$words);
5 var_dump($keyExisted1);     //boolean true
6 echo "<br/>";
7 var_dump($keyExisted2);     //boolean false
8 ?>

输出:

D:\wampServer\www\Apache服务器主目录\practise\例程.php:5:boolean true

D:\wampServer\www\Apache服务器主目录\practise\例程.php:7:boolean false

 

5. array_search() 函数

程序:

 1 <?php
 2 $words = array(".NET"=>"Microsoft","JAVA"=>"SUN","JSP"=>"SUN");
 3 $searchKey1 = array_search("SUN",$words);
 4 var_dump($searchKey1);  //string 'JAVA' (length=4)
 5 echo "<br/>";
 6 $searchKey2 = array_search("microsoft", $words);
 7 var_dump($searchKey2);  //boolean false
 8 echo "<br/>";
 9 
10 $numbers = array("PI"=>"3.14","直角"=>"90");
11 $searchKey3 = array_search(90, $numbers);
12 $searchKey4 = array_search(90, $numbers,TRUE);   //会比较数据类型是否相同
13 var_dump($searchKey3);  //string '直角' (length=6)
14 echo "<br/>";
15 var_dump($searchKey4);  //boolean false
16 ?>

输出:

D:\wampServer\www\Apache服务器主目录\practise\例程.php:4:string 'JAVA' (length=4)

D:\wampServer\www\Apache服务器主目录\practise\例程.php:7:boolean false

D:\wampServer\www\Apache服务器主目录\practise\例程.php:13:string '直角' (length=6)

D:\wampServer\www\Apache服务器主目录\practise\例程.php:15:boolean false

 

6. array_unique() 函数

程序:

1 <?php
2 $colors = array("a"=>"green","red","b"=>"green","blue","red");
3 $colorUnique = array_unique($colors);   //Array ( [a] => green [0] => red [1] => blue )
4 print_r($colorUnique);
5 echo "<br/>";
6 $input = array(4,"4","3",4,3,"3");
7 $inputUnique = array_unique($input);    //Array ( [0] => 4 [2] => 3 )
8 print_r($inputUnique);      
9 ?>

输出:

Array ( [a] => green [0] => red [1] => blue ) 
Array ( [0] => 4 [2] => 3 )

 

来源:https://www./content-1-554501.html

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多