PHP查找与搜索数组元素方法总结,php查找数组元素
本文实例讲述了PHP查找与搜索数组元素方法。分享给大家供大家参考。具体分析如下:
查找、筛选与搜索数组元素是数组操作的一些常见功能。下面来介绍一下几个相关的函数。
in_array()函数
in_array()函数在一个数组汇总搜索一个特定值,如果找到这个值返回true,否则返回false。其形式如下:
boolean in_array(mixed needle,array haystack[,boolean strict]);
来看下面的例子,查找变量apple是否已经在数组中,如果在,则输出一段信息:
$fruit = "apple"; $fruits = array("apple","banana","orange","pear"); if( in_array($fruit,$fruits) ) echo "$fruit 已经在数组中";
第三个参数可选,它强制in_array()在搜索时考虑类型。
array_key_exists()函数
如果在一个数组中找到一个指定的键,函数array_key_exists()返回true,否则返回false。其形式如下:
boolean array_key_exists(mixed key,array array);
下面的例子将在数组键中搜索apple,如果找到,将输出这个水果的颜色:
$fruit["apple"] = "red"; $fruit["banana"] = "yellow"; $fruit["pear"] = "green"; if(array_key_exists("apple", $fruit)){ printf("apple's color is %s",$fruit["apple"]); }
执行这段代码得到的结果:
复制代码 代码如下:apple's color is red
array_search()函数
array_search()函数在一个数组中搜索一个指定的值,如果找到则返回相应的键,否则返回false。其形式如下:
mixed array_search(mixed needle,array haystack[,boolean strict])
下面的例子在$fruits中搜索一个特定的日期(December 7),如果找到,则返回相应州的有关信息:
$fruits["apple"] = "red"; $fruits["banana"] = "yellow"; $fruits["watermelon"]="green"; $founded = array_search("green", $fruits); if($founded) printf("%s was founded on %s.",$founded, $fruits[$founded])
程序运行结果如下:
复制代码 代码如下:watermelon was founded on green.
array_keys()函数
array_keys()函数返回一个数组,其中包含所搜索数组中找到的所有键。其形式如下:
array array_keys(array array[,mixed search_value])
如果包含可选参数search_value,则只会返回与该值匹配的键。下面的例子将输出$fruit数组中找到的所有数组:
$fruits["apple"] = "red"; $fruits["banana"] = "yellow"; $fruits["watermelon"]="green"; $keys = array_keys($fruits); print_r($keys);
程序运行结果如下:
复制代码 代码如下:Array ( [0] => apple [1] => banana [2] => watermelon )
array_values()函数
array_values()函数返回一个数组中的所有值,并自动为返回的数组提供数值索引。其形式如下:
array array_values(array array)
下面的例子将获取$fruits中找到的各元素的值:
$fruits["apple"] = "red"; $fruits["banana"] = "yellow"; $fruits["watermelon"]="green"; $values = array_values($fruits); print_r($values);
程序运行结果如下:
复制代码 代码如下:Array ( [0] => red [1] => yellow [2] => green )
希望本文所述对大家的php程序设计有所帮助。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft