Home >Backend Development >PHP Problem >How to check if a number is in an array in php

How to check if a number is in an array in php

PHPz
PHPzOriginal
2023-04-20 09:10:48605browse

Array operations are a common requirement in PHP. When we use PHP to perform array operations, sometimes we need to determine whether a number is in the array.

This article will introduce in detail how to use PHP to perform query operations. Among them, the following content will be included:

  1. The definition and use of arrays in PHP.
  2. How to query whether a number is in an array in PHP.
  3. How to optimize the query process.

1. The definition and use of arrays in PHP

In PHP, arrays are a very important data structure and are widely used in data operations, parameter transfer and return. value, etc. Arrays can store multiple data of the same type, each with its own location identifier (key).

Arrays in PHP can be defined in two ways:

  1. Using the array() function. For example:
$arr = array(1,2,3,4,5);
  1. Use [] to create an empty array, and then use $arr[] = value; to add array elements. For example:
$arr = [];
$arr[] = 1;
$arr[] = 2;
$arr[] = 3;

When using an array, we can access the data of the array through the keys of the array elements. The key of the array can be a number or a string, for example:

$arr = array(
    "name" => "Tom",
    "age" => 18,
    "sex" => "男",
);

echo $arr["name"]; //输出 Tom
echo $arr["age"]; //输出 18
echo $arr["sex"]; //输出 男

2. How to check whether a number is in the array in PHP

In PHP, how to check whether a number is in the array There are many methods in arrays, for example:

  1. Use the in_array() function. This function searches the array for the specified value and returns true or false. For example:
$arr = array(1,2,3,4,5);
if(in_array(3,$arr)) {
    echo "3 在数组中";
} else {
    echo "3 不在数组中";
}
  1. Use the array_search() function. This function looks for the specified value in the array and returns the key of the value if found, otherwise it returns false. For example:
$arr = array(1,2,3,4,5);
$result = array_search(3,$arr);
if($result !== false) {
    echo "3 在数组中,对应的键为:" . $result;
} else {
    echo "3 不在数组中";
}
  1. Use isset() function. This function is used to check whether the specified key exists in the array and returns true if it exists, otherwise it returns false. For example:
$arr = array(
    "name" => "Tom",
    "age" => 18,
    "sex" => "男",
);
if(isset($arr["age"])) {
    echo "存在 age 键";
} else {
    echo "不存在 age 键";
}

The above methods can all query whether a number is in the array. Please choose the appropriate method according to actual needs.

3. How to optimize the query process

In PHP, in order to improve query efficiency, you can use the following two methods to optimize the query process:

  1. Use The isset() function determines whether the index exists

Since the isset() function is a built-in function of PHP, the query speed will be faster. When we use the in_array() function or array_search() function, they both need to traverse the entire array to perform the search operation. In comparison, the isset() function is faster.

Therefore, we can first use the isset() function to determine whether the index exists. If it exists, it means that a certain number is in the array, and we can directly return true to improve efficiency.

For example:

//1、使用 in_array() 函数进行查找
$arr = array(1,2,3,4,5);
if(in_array(3,$arr)) {
    echo "3 在数组中";
} else {
    echo "3 不在数组中";
}

//2、使用 isset() 函数进行查找
$arr = array(1,2,3,4,5);
if(isset($arr[2])) {
    echo "3 在数组中";
} else {
    echo "3 不在数组中";
}
  1. Convert the array to a string and use the strpos() function to find it

For some specific arrays, we can It is converted to a string and then looked up using the strpos() function. This method is faster than traversing the array for query.

For example:

//将数组转换为字符串
$arr = array(1,2,3,4,5);
$str = implode(",", $arr);

//使用 strpos() 函数进行查找
if(strpos($str, "3") !== false) {
  echo "3 在数组中";
} else {
  echo "3 不在数组中";
}

It should be noted that since this method will convert the entire array into a string, it is suitable for arrays that require multiple queries and is not suitable for arrays with very large amounts of data. Case.

Summary:

This article introduces the definition and use of arrays in PHP, as well as various methods to query whether a number is in an array. In actual use, the most appropriate method should be selected according to the specific situation. In order to improve query efficiency, you can also use the isset() function and strpos() function to optimize the query process.

The above is the detailed content of How to check if a number is in an array in php. For more information, please follow other related articles on the PHP Chinese website!

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