Home  >  Article  >  Backend Development  >  Detailed explanation of PHP in_array() function usage

Detailed explanation of PHP in_array() function usage

王林
王林Original
2023-06-27 10:18:402701browse

There are many practical functions in PHP, among which the in_array() function is one of the more commonly used functions. This article will introduce the usage of in_array() function in detail.

First of all, the in_array() function is used to check whether a value exists in an array. If it exists, return true; if it does not exist, return false.

The syntax of the in_array() function is as follows:

in_array($needle, $haystack, $strict)

Parameter explanation:

  • $needle: the value to be found.
  • $haystack: the array where it is located.
  • $strict: Optional parameter. When set to true, it will check whether the type of needle is the same as that in haystack. Default is false.

Here are some examples:

$arr1 = array("apple", "banana", "orange");
echo in_array("apple", $arr1); // 输出 1 (true)
echo in_array("lemon", $arr1); // 输出空 (false)

In the above example, we can see that the in_array() function successfully found "apple" and returned true; and for "lemon" was not found and false was returned.

Next, let’s look at some more complex usage.

1. Check if multiple values ​​exist

In some cases, we need to check if multiple values ​​exist. This can be achieved by looping in_array() function:

$names = array("Alice", "Bob", "Charlie", "Dave", "Eve");
$search = array("Alice", "John");

foreach ($search as $name) {
    if (in_array($name, $names)) {
        echo "$name exists in names.";
    } else {
        echo "$name does not exist in names.";
    }
}

In this example, we check whether "Alice" and "John" exist in the $names array in a loop. For example, "Alice exists in names." and "John does not exist in names." are output.

2. Use strict mode

If we want to ensure that the checked value exactly matches the value type and value in the array, we can use strict mode. In this case, the value is considered to exist only if the two types and values ​​match exactly. Strict mode can be enabled by setting the third parameter to true.

$numbers = array("1", 2, 3);
echo in_array(1, $numbers);    // 输出 1(true)
echo in_array(1, $numbers, true);  // 输出 空(false)

In the above example, if strict mode is not used, "1" and the integer 1 will be considered equal. However, if strict mode is used, the types will be taken into account, so they will be considered unequal.

3. Use the in_array() function to implement simple permission control

The other in_array() function is used to implement simple permission control. For example, let's say we have a set of user roles and a set of roles that allow access to a page. We can use the in_array() function to check whether the user's role allows access to the page:

$user_role = "manager";   // 用户角色为manager
$allowed_roles = array("manager", "admin", "superadmin");  // 允许用户角色

if (in_array($user_role, $allowed_roles)) {
    // 用户允许访问
} else {
    // 用户不允许访问
}

In the above example, we check whether $user_role is included in the $allowed_roles array. If included, the user is allowed access, otherwise not.

Summary:

The in_array() function is a very practical function in PHP, which can easily check whether a value exists in an array. We can also do more complex usage with loops and strict mode. By mastering the usage of the in_array() function, we can improve the efficiency of writing PHP code.

The above is the detailed content of Detailed explanation of PHP in_array() function usage. 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