search
Homephp教程php手册PHP函数in_array()使用详解

这篇文章主要介绍了PHP函数in_array()使用详解,分别对普通使用、使用第三个参数、克隆对象、多维数组等给出了例子,需要的朋友可以参考下

PHP有一个系统函数is_array()可以判断一个值是否在数组中。
语法如下:

复制代码 代码如下:


in_array(value,array,type)
return boolen


参数说明:
value :要搜索的值
array : 被搜索的数组
type : 类型,true全等 ,,false非全等(默认)

示例一:普通使用

代码:

复制代码 代码如下:


$str = 1;
 
$arr = array(1,3,5,7,9);
 
$boolvalue = in_array($str,$arr);
 
var_dump($boolvalue);


执行结果:

复制代码 代码如下:


bool(true)


实例二:使用第三个参数
非全等
代码:

复制代码 代码如下:


$str = '1';
 
$arr = array(1,3,5,7,9);
 
$boolvalue = in_array($str,$arr,false);
 
var_dump($boolvalue);


执行结果:

复制代码 代码如下:


bool(true)


全等
代码:

复制代码 代码如下:


$str = '1';
 
$arr = array(1,3,5,7,9);
 
$boolvalue = in_array($str,$arr,true);
 
var_dump($boolvalue);


执行结果:

复制代码 代码如下:


bool(false)


实例三:克隆对象
代码:

复制代码 代码如下:


class a {
    public $a = 1; 
    public function fun(){
        return $this->a;
    }
}
 
class b {
    public $a = 2; 
    public function fun(){
        return $this->a;
    }
}
 
$a = new a();
$b = new b();
 
$c = clone $a;
 
$arr = array($a,$b);
 
$boolvalue = in_array($c,$arr,false);
 
var_dump($boolvalue);


执行结果:

复制代码 代码如下:


bool(true)


 
代码:

复制代码 代码如下:


class a {
    public $a = 1; 
    public function fun(){
        return $this->a;
    }
}
 
class b {
    public $a = 2; 
    public function fun(){
        return $this->a;
    }
}
 
$a = new a();
$b = new b();
 
$c = clone $a;
 
$arr = array($a,$b);
 
$boolvalue = in_array($c,$arr,true);
 
var_dump($boolvalue);


执行结果:

复制代码 代码如下:


bool(false)


示例四:多维数组
代码:

复制代码 代码如下:


$str = 10;
 
$arr = array(
    array(1,2,3,4),
    array(5,6,7,8,9),
    10
);
 
$boolvalue = in_array($str,$arr);
 
var_dump($boolvalue);


执行结果:

复制代码 代码如下:

bool(true)


 
代码:

复制代码 代码如下:


$str = 10;
 
$arr = array(
    array(1,2,3,4),
    array(5,6,7,8,9,10),
);
 
$boolvalue = in_array($str,$arr);
 
var_dump($boolvalue);


执行结果:

复制代码 代码如下:


bool(false)

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools