Home > Backend Development > PHP Problem > How to determine whether an array exists in php

How to determine whether an array exists in php

PHPz
Release: 2023-04-20 09:37:11
Original
620 people have browsed it

In PHP, we can use the in_array() function to determine whether a value exists in an array. The format of this function is as follows:

bool in_array(mixed $needle, array $haystack [, bool $strict = FALSE ])
Copy after login

Among them, $needle is the value to be searched, $haystack is the array to be searched, and $strict is an optional parameter, indicating whether to enable strict mode (default is FALSE).

If the value exists in the array, the return value is TRUE, otherwise it returns FALSE.

The sample code is as follows:

<?php
// 示例数组
$arr = array("apple", "orange", "banana");

// 判断orange是否存在于数组中
if (in_array("orange", $arr)) {
    echo "orange存在于数组中。";
} else {
    echo "orange不存在于数组中。";
}

// 判断pear是否存在于数组中
if (in_array("pear", $arr)) {
    echo "pear存在于数组中。";
} else {
    echo "pear不存在于数组中。";
}
?>
Copy after login

The output of the above code is as follows:

orange存在于数组中。
pear不存在于数组中。
Copy after login

In addition to the in_array() function, PHP also provides other functions to determine whether array elements exist, such as array_key_exists(), in_array(), etc. Just choose the appropriate function according to your specific needs.

The above is the detailed content of How to determine whether an array exists 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