ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-11-10 09:00:37  阅读:343  来源: 互联网

标签:practise 例程 主目录 boolean 数组 Apache array PHP 处理函数


以下为学习孔祥盛主编的《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 )

 

标签:practise,例程,主目录,boolean,数组,Apache,array,PHP,处理函数
来源: https://www.cnblogs.com/xiaoxuStudy/p/11827164.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有