PHP搜索数组的实现方法介绍
array_unshift() //在数组的头部添加元素,也就是把一个新的元素插入到数组,相应索引还自动改变
array_push() //在数组尾部添加元素
array_shift() //在数组头部删除元素
array_pop() //在数组尾部删除元素
这几个函数的用法基本差不多,在PHP的帮助手册中可以得到具体的用法,在此就不多啰嗦了。
PHP搜索数组的代码示例:
in_array()函数在数组中搜索一个特定的值,如果找到则返回TRUE,否则返回FALSE。以下的示例来自PHP帮助手册。
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><span> ?php </span></span></span></li> <li> <span>$</span><span class="attribute">os</span><span> = </span><span class="attribute-value">array</span><span>("Mac", "NT", "Irix", "Linux"); </span> </li> <li class="alt"><span>if (in_array("Irix", $os)) { </span></li> <li><span>echo "Got Irix"; </span></li> <li class="alt"><span>} </span></li> <li><span>if (in_array("mac", $os)) { </span></li> <li class="alt"><span>echo "Got mac"; </span></li> <li><span>} </span></li> <li class="alt"> <span class="tag">?></span><span> </span> </li> </ol>
要特别说明的,in_array()在搜索时是区分大小的,所以以上的例子只会返回“Got Irix”。你也可以用数组作为子串在数组中进行搜索,更多的实例请查看帮助手册。
PHP搜索数组之搜索关联数组
如果你搜索的数组是一个关联数组,就要使用array_key_exists(),其返回值跟in_array()一样。同样我们引用PHP帮助手册的例子来说明:
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><span> ?php </span></span></span></li> <li> <span>$</span><span class="attribute">search_array</span><span> = array('first' =</span><span class="tag">></span><span> 1, 'second' =</span><span class="tag">></span><span> 4); </span> </li> <li class="alt"><span>if (array_key_exists('first', $search_array)) { </span></li> <li><span>echo "The 'first' element is in the array"; </span></li> <li class="alt"><span>} </span></li> <li> <span class="tag">?></span><span> </span> </li> </ol>
相信这么一段代码就不要解释了吧,一看就能够明白,其结果:The ‘first’ element is in the array。这说明”first”存在于数组”search_array”中。

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

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

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 English version
Recommended: Win version, supports code prompts!

SublimeText3 Linux new version
SublimeText3 Linux latest version
