Home > Backend Development > PHP Problem > How to determine whether it is an element in an array in php

How to determine whether it is an element in an array in php

PHPz
Release: 2023-04-20 09:39:28
Original
511 people have browsed it

In PHP, determining whether an element exists in an array is a common problem. In this article, we explore several ways to make this determination.

Use the in_array() function

in_array() is a built-in function in PHP, which can be used to determine whether a value exists in an array. The function is called as follows:

in_array($value, $array);
Copy after login

Among them, $value is the value to be found, and $array is the target array. After calling this function, if $value exists in $array, it returns true, otherwise it returns false.

The following is sample code using the in_array() function:

$array = array('foo', 'bar', 'baz');
if (in_array('foo', $array)) {
    echo 'foo is in the array';
} else {
    echo 'foo is not in the array';
}
Copy after login

In this example, 'foo' is the value to be found and $array is the target array. Since 'foo' exists in $array, the above code will output 'foo is in the array'.

Using the array_search() function

array_search() is another built-in function in PHP, which can be used to find the position of a value in an array. The function is called as follows:

array_search($value, $array);
Copy after login

Among them, $value is the value to be found, and $array is the target array. After calling this function, if $value exists in $array, the key value of the value in the array is returned, otherwise false is returned.

The following is sample code using the array_search() function:

$array = array('foo', 'bar', 'baz');
$key = array_search('foo', $array);
if ($key !== false) {
    echo 'foo is at position ' . $key;
} else {
    echo 'foo is not in the array';
}
Copy after login

In this example, 'foo' is the value to be found, and $array is the target array. Since 'foo' exists in $array and its key value is 0, the above code will output 'foo is at position 0'.

It should be noted that the array_search() function will stop searching after finding the first match. That is, if there are multiple identical values, the array_search() function will only return the key value of the first matching item.

Using the in_array() function and strict parameters

In PHP's in_array() function, you can also use the strict parameter to perform strict type matching. By default, this function performs loose matching, that is, if the elements in the array and the value to be found do not match in type, but their values ​​are equal, the in_array() function will still return true.

After using the strict parameter, the in_array() function will perform strict type matching. If the element in the array does not match the type of the value being searched, the in_array() function will return false.

The following is an example code using the in_array() function and the strict parameter:

$array = array(0, 1, '2');
if (in_array(2, $array, true)) {
    echo '2 is in the array';
} else {
    echo '2 is not in the array';
}
Copy after login

In this example, the value we are looking for is 2, and $array contains 0, 1 and '2 'These three elements. Since we used the strict parameter, the in_array() function performs strict type matching. Since 2 and '2' do not match in type, the above code will output '2 is not in the array' even though their values ​​are equal.

Using the array_key_exists() function

array_key_exists() is another built-in function in PHP, which can be used to determine whether a key exists in an array. The function is called as follows:

array_key_exists($key, $array);
Copy after login

where $key is the key to be searched and $array is the target array. After calling this function, if $key exists in $array, it returns true, otherwise it returns false.

The following is sample code using the array_key_exists() function:

$array = array('foo' => 'bar', 'baz' => 'qux');
if (array_key_exists('foo', $array)) {
    echo 'foo is in the array';
} else {
    echo 'foo is not in the array';
}
Copy after login

In this example, 'foo' is the key to be found, and $array is the target array. Since 'foo' exists in $array, the above code will output 'foo is in the array'.

It should be noted that the array_key_exists() function can only be used to determine whether the key exists, but not whether the value exists.

Conclusion

Determining whether an element exists in an array is a very common problem. In this article, we explore four ways to make this determination. By mastering these methods, you can write PHP programs more easily and handle array operations more efficiently.

The above is the detailed content of How to determine whether it is an element in an array in php. For more information, please follow other related articles on the PHP Chinese website!

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