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

数组检索函数

1. array_keys() 函数

程序:

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

输出:

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

2. array_values() 函数

程序:

 <?php
$interests[2] = "music";
$interests[5] = "movie";
$interests[1] = "computer";
$interests[] = "software";
$interests[] = "computer";
$values = array_values($interests);
print_r( $values );
?>

输出:

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

3. in_array() 函数

程序:

 <?php
$words = array("JAVA","PHP",".NET");
$javaExisted = in_array("JAVA",$words);
$phpExisted = in_array("PHP",$words);
var_dump($javaExisted); //boolean true
echo "<br/>";
var_dump($phpExisted); //boolean true
echo "<br/>"; $numbers = array('1.10',12.4,1.13);
$numExisted1 = in_array(1.10,$numbers);
$numExisted2 = in_array(1.10,$numbers,TRUE); //会比较数据类型是否相同
var_dump($numExisted1); //boolean true
echo "<br/>";
var_dump($numExisted2); //boolean false
?>

输出:

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() 函数

程序:

 <?php
$words = array( "SUN"=>"JAVA","Microsoft"=>".NET" );
$keyExisted1 = array_key_exists("SUN",$words);
$keyExisted2 = array_key_exists("sun",$words);
var_dump($keyExisted1); //boolean true
echo "<br/>";
var_dump($keyExisted2); //boolean false
?>

输出:

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

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

5. array_search() 函数

程序:

 <?php
$words = array(".NET"=>"Microsoft","JAVA"=>"SUN","JSP"=>"SUN");
$searchKey1 = array_search("SUN",$words);
var_dump($searchKey1); //string 'JAVA' (length=4)
echo "<br/>";
$searchKey2 = array_search("microsoft", $words);
var_dump($searchKey2); //boolean false
echo "<br/>"; $numbers = array("PI"=>"3.14","直角"=>"90");
$searchKey3 = array_search(90, $numbers);
$searchKey4 = array_search(90, $numbers,TRUE); //会比较数据类型是否相同
var_dump($searchKey3); //string '直角' (length=6)
echo "<br/>";
var_dump($searchKey4); //boolean false
?>

输出:

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() 函数

程序:

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

输出:

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

5_PHP数组_3_数组处理函数及其应用_6_数组检索函数的更多相关文章

  1. php extract 函数的妙用 数组键名为声明为变量,键值赋值为变量内容

    extract 函数的妙用 数组键名为声明为变量,键值赋值为变量内容 它的主要作用是将数组展开,键名作为变量名,元素值为变量值,可以说为数组的操作提供了另外一个方便的工具

  2. Atitit main函数的ast分析  数组参数调用的ast astview解析

    Atitit main函数的ast分析  数组参数调用的ast astview解析 1.1. Xxcls.main(new String[]{"","bb"}) ...

  3. 面试题-->写一个函数,返回一个数组中所有元素被第一个元素除的结果

    package com.rui.test; import java.util.Random; /** * @author poseidon * @version 1.0 * @date:2015年10 ...

  4. php函数、php定义数组和数组遍历

    <?php //php函数//1.简单函数//四要素:返回类型,函数名,参数列表,函数体 /*function Show(){ echo "hello";} Show();* ...

  5. C++ 数组长度 以及 数组名作为参数传递给函数 以及 为什么不在子函数中求数组长度

    在看排序,首先是插入排序,思路理清后想用代码实现,然后问题来了: 如何求数组长度? 如果没记错,在Java中应该是有直接可用的方法的, Python中(序列)也有.len,在C/C++中,字符串倒是有 ...

  6. C语言 数组做函数参数不传数组个数的遍历方法

    //数组做函数参数不传数组个数的遍历方法 #include<stdio.h> #include<stdlib.h> #include<string.h> void ...

  7. PHP基础语法: echo,var_dump, 常用函数:随机数:拆分字符串:explode()、rand()、日期时间:time()、字符串转化为时间戳:strtotime()可变参数的函数:PHP里数组长度表示方法:count($attr[指数组]);字符串长度:strlen($a)

    PHP语言原理:先把代码显示在源代码中,再通过浏览器解析在网页上 a. 1.substr;  //用于输出字符串中,需要的某一部分 <?PHP $a="learn php"; ...

  8. 思维导图(自己整理,希望对大家有用):JavaScript函数+canvas绘图+Array数组

    1.javascript函数: 2.Array数组: 3.canvas绘图:

  9. C语言基础知识点整理(函数/变量/常量/指针/数组/结构体)

    函数 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ...

随机推荐

  1. EasyExcel写入百万级数据到多sheet---非注解方式

    EasyExcel是什么? 快速.简单避免OOM的java处理Excel工具 一.项目需求 从mongo库中查询数据,导出到excel文件中.但是动态导出的excel有多少列.列名是什么.有多少she ...

  2. php 把数字转化为大写中文—升级版

    继上篇之后,发现某同事悄悄改了新版本,于是被我偷偷保存起来了,功能一样,不过他的比较短小,emmm.放了快一年了,悄悄放到博客里面. 功能需求在另一篇博客里 <?php function cny ...

  3. 【大数据】安装关系型数据库MySQL 安装大数据处理框架Hadoop

    作业要求来自:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/3161 1.安装Mysql 使用命令  sudo apt-get ins ...

  4. 第06组 Beta冲刺(2/5)

    队名:拾光组 组长博客链接 作业博客链接 团队项目情况 燃尽图(组内共享) 组长:宋奕 过去两天完成了哪些任务 维护后端代码 学习后端架构 GitHub签入记录 接下来的计划 维护后端代码,跟进组员完 ...

  5. 东站七雄保C位!论三线楼市网红板块的自我修养

    不对!东站板块才是伍家岗的C位.这里有东站七雄! 前些天发了一篇城东C位之路的文章,居然引发了诸葛说房聊天群内大佬的激烈纷争.公说公有理婆说婆有理,一时争的是不可开交,大有约架之势.所以我决定提前写& ...

  6. Innodb的redo log block

  7. pycharm安装pyinstaller将pygame打包成exe

    首先,使用pycharm自带的下载包工具,File-Settings-Project Interpreter,如图: 安装完成后,发现安装到了Python根目录下,我的在C:\python34\Scr ...

  8. 【414 error】nginx GET请求过长导致414错误

    server{ ... } 在上面一段配置中添加如下两行 client_header_buffer_size 5120k; large_client_header_buffers 5120k; 并重启 ...

  9. JS字符串数字前面加加号会变成数字类型

    JS中一个字符串中只有数字,如果该字符串前面加了个加号,这个数值就变成了number类型.如本文测试中,用lodop打印二维码,最后一个参数是一个字符串,在前面加了加号和不在前面加加号,通过控制台输出 ...

  10. jenkins自动化回滚阿里云k8s应用版本

    jenkins 服务器需先保留着原先构建的不同版本的应用镜像 [root@jenkins sh]# docker images|grep "maintain" registry-v ...