Home > Backend Development > PHP Problem > How to query in php whether it contains a specified array value

How to query in php whether it contains a specified array value

青灯夜游
Release: 2023-03-15 10:52:01
Original
2693 people have browsed it

Query method: 1. Use the in_array() function, the syntax "in_array(value, array)", if included, return TRUE; 2. Use the array_search() function, the syntax "array_search(value, array)" , if included, returns the corresponding key name.

How to query in php whether it contains a specified array value

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

php query whether it is included Specify the array value

Method 1: Use the in_array() function

The in_array() function searches whether the specified value exists in the array. Returns TRUE if the value is found in the array, FALSE otherwise.

<?php
header("Content-type:text/html;charset=utf-8");
$people = array("Bill", "Steve", "Mark", "David");

if (in_array("23", $people, TRUE))
  {
  echo "匹配已找到<br>";
  }
else
  {
  echo "匹配未找到<br>";
  }
if (in_array("Mark",$people, TRUE))
  {
  echo "匹配已找到<br>";
  }
else
  {
  echo "匹配未找到<br>";
  }

if (in_array(23,$people, TRUE))
  {
  echo "匹配已找到<br>";
  }
else
  {
  echo "匹配未找到<br>";
  }
?>
Copy after login

How to query in php whether it contains a specified array value

Method 2: Use the array_search() function

array_search() function to search for a key value in the array, and Return the corresponding key name.

If the specified key value is found in the array, return the corresponding key name, otherwise return FALSE. If a key value is found more than once in the array, the key name matching the first found key value is returned.

<?php
header("Content-type:text/html;charset=utf-8");
$a = array("2", 1, "3", 4);

if (array_search(1,$a,true)){
  echo "匹配已找到<br>";
}
else{
  echo "匹配未找到<br>";
}
if (array_search("1",$a,true)){
  echo "匹配已找到<br>";
}else{
  echo "匹配未找到<br>";
}
if (array_search(5,$a,true)){
  echo "匹配已找到<br>";
}else{
  echo "匹配未找到<br>";
}
?>
Copy after login

How to query in php whether it contains a specified array value

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to query in php whether it contains a specified array value. 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