How to determine if a value is in an array in php

PHPz
Release: 2023-04-25 18:03:42
Original
580 people have browsed it

PHP, as a scripting language widely used in web development, has strong data processing capabilities, among which arrays are one of the important data types in PHP. In PHP, we often need to determine whether a value exists in an array, so determining whether a value in PHP is in an array is a very common problem. This article will explore this issue in depth.

1. Introduction to PHP arrays

In PHP, arrays are a commonly used data type with the following characteristics:

  1. PHP arrays are ordered lists , each element has a unique key value (i.e. subscript), which can be an integer or a string.
  2. PHP array key values can grow automatically.
  3. The value of a PHP array can be any type of data, including integer, floating point, Boolean, string, object, etc.

For example, the following is a simple PHP array:

$cities=array("Beijing","Shanghai","Guangzhou","Shenzhen");
Copy after login

In this array,$citiesis an array variable name,array( "Beijing","Shanghai","Guangzhou","Shenzhen")is an array composed of four elements. Each element corresponds to a city name of string type, and the subscripts are 0, 1, and 2, 3.

2. How to determine whether a value is in an array in PHP

Now suppose we have an array$arrand a value$value, We need to determine whether this value is in the array. There are several ways to accomplish this in PHP, including using thein_array()function, using thearray_search()function, usingisset()andarray_key_exists()functions, etc., will be introduced separately below.

  1. Use thein_array()function to determine whether the value is in the array

in_array()The function is used to check a value exists in an array and returns a Boolean value. ReturnsTrueif$valueexists in$arr, otherwise returnsFalse.

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

Here is an example:

$fruits=array("apple","banana","orange","kiwi"); if(in_array("banana",$fruits)){ echo "查找到了值!"; }else{ echo "未查找到值."; }
Copy after login

This code will outputThe value was found!.

  1. Use thearray_search()function to determine whether the value is in the array

array_search()The function is used to try to search in the array Finds an element in and returns its key (i.e. subscript). If$valueexists in$arr, returns the key of this element, otherwise returnsFalse.

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

Here is an example:

$fruits=array("apple","banana","orange","kiwi"); $index=array_search("banana",$fruits); echo $index;
Copy after login

This code will output 1 becausebananacorresponds to the second element in the$fruitsarray , its subscript is 1.

It should be noted that if$valuecorresponds to multiple elements, only the subscript of the first element encountered will be returned.

  1. Useisset()andarray_key_exists()functions to determine whether the value is in the array

##isset( )function is used to detect whether the variable is set. If the variable exists and the value is notNULL, it returnsTrue, otherwise it returnsFalse.

isset($arr[$key]);
Copy after login

array_key_exists()Function is used to check whether the specified key or index exists in the array. If$keyexists in$arr, returnsTrue, otherwise returnsFalse.

array_key_exists($key,$arr);
Copy after login
The following is an example:

$fruits=array("apple"=>1,"banana"=>2,"orange"=>3,"kiwi"=>4); if(isset($fruits["banana"])){ echo "查找到了值!"; }else{ echo "未查找到值."; }
Copy after login
This code will output

The value was found!, because the key value in the array isbananaElements.

3. Comparison of three methods in PHP

Using the

in_array()function and thearray_search()function are more common methods. They are also relatively efficient methods.

in_array()The function returns a Boolean value, there is no need to query the position of the element in the array, and it is faster. Thearray_search()function returns the index of the element in the array, which is slightly slower.

Use the

isset()function and thearray_key_exists()function is more commonly used for associative arrays (that is, arrays whose subscripts are strings). The difference between these two functions is that theisset()function returnsTruewhen the target value exists, and still returnsFalsewhen the target value does not exist; andarray_key_exists()The function returnsFalsewhen the target value does not exist, andTrueonly when the target value exists.

4. Summary

Determining whether a value exists in an array is a common requirement in PHP. This article introduces three methods in PHP: using

in_array()function, using thearray_search()function and using theisset()andarray_key_exists()functions. These methods are relatively simple in implementation and can be selected and used according to needs. At the same time, other methods or algorithms can also be selected according to the actual situation. Either way, the key is a trade-off between correctness and efficiency.

The above is the detailed content of How to determine if a value is 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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!