PHP determines whether a certain value in an array exists

王林
Release: 2023-04-07 13:00:01
Original
6516 people have browsed it

PHP determines whether a certain value in an array exists

PHP in_array() function checks whether a certain value exists in the array. If it exists, it returns TRUE, otherwise it returns FALSE.

Syntax:

bool in_array( mixed needle, array array [, bool strict] )
Copy after login

Parameter description:

PHP determines whether a certain value in an array exists

Example:

<?php
$arr_a = array("a", "b", "c", 1);
if(in_array("a", $arr_a)){
  echo &#39;字符 a 在 $arr_a 数组中存在&#39;;
} else {
  echo &#39;字符 a 在 $arr_a 数组中不存在&#39;;
}
?>
Copy after login

Character a Strictly checked example exists in $arr_a array:

<?php
$arr_a = array("a", "b", "c", 1);
if(in_array("1", $arr_a, TRUE)){
  echo &#39;字符 1 在 $arr_a 数组中存在&#39;;
} else {
  echo &#39;字符 1 在 $arr_a 数组中不存在&#39;;
}
?>
Copy after login

Character 1 Example of array as needle that does not exist in $arr_a array:

<?php
$arr_a = array(array("a", "b"), 1, 2);
$arr_b = array("a", "b");
if(in_array($arr_b, $arr_a)){
    echo &#39;数组 $arr_b 在 $arr_a 数组中存在&#39;;
} else {
    echo &#39;数组 $arr_b 在 $arr_a 数组中不存在&#39;;
}
?>
Copy after login

Array $arr_b exists in $arr_a array.

Recommended tutorial: PHP video tutorial

The above is the detailed content of PHP determines whether a certain value in an array exists. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template