Home > Backend Development > PHP Tutorial > PHP uses in_array function to check whether a value exists in an array_PHP tutorial

PHP uses in_array function to check whether a value exists in an array_PHP tutorial

WBOY
Release: 2016-07-13 10:00:21
Original
913 people have browsed it

PHP uses the in_array function to check whether a certain value exists in the array

This article mainly introduces PHP using the in_array function to check whether a certain value exists in the array, with a more detailed analysis The function, definition and related usage skills and precautions of the in_array function are explained. It has certain reference value. Friends in need can refer to it

The example in this article describes how PHP uses the in_array function to check whether a certain value exists in an array. Share it with everyone for your reference. The specific analysis is as follows:

PHP uses the in_array() function to check whether a certain value exists in the array. If it exists, it returns TRUE, otherwise it returns FALSE. It is very easy to use. Let me introduce the in_array() function to you in depth.

When I was writing a piece of code in PHP recently, I needed to determine whether a certain value was in another set of values. The in_array function is used to check whether a certain value exists in the array. It is relatively vague to understand directly through concepts, and its function can be understood through specific examples.

The syntax is as follows:

?

1

bool in_array( mixed needle, array array [, bool strict] )

1
bool in_array( mixed needle, array array [, bool strict] )

参数 说明
needle 需要在数组中搜索的值,如果是字符串,则区分大小写
array 需要检索的数组
strict 可选,如果设置为 TRUE ,则还会对 needle 与 array 中的值类型进行检查

Parameter description:

Example 1:

1

2

3

4

5

6

7

8

9

$os = array("Mac", "NT", "Irix", "Linux");

if (in_array("Irix", $os)) {

echo "Got Irix";

}

if (in_array("mac", $os)) {

echo "Got mac";

}

?>

?

1

2

3

4

5

6

1

2

3

4

5

6

$europe = array("美国","英国","法国","德国","意大利","西班牙","丹麦");

if (in_array("美国",$europe)) {

echo "True";

}

?>

7 8

9

$os = array("Mac", "NT", "Irix", "Linux");

if (in_array("Irix", $os)) {

1

2

3

4

5

6

7

8

9

$a = array('1.10', 12.4, 1.13);

if (in_array('12.4', $a, true)) {

echo "'12.4' found with strict check ";

}

if (in_array(1.13, $a, true)) {

echo "1.13 found with strict check ";

}

?>

echo "Got Irix"; } if (in_array("mac", $os)) { echo "Got mac"; } ?> The execution result of the above code is: Got Irix The second condition fails because in_array() is case sensitive. Example 2: ?
1 2 3 4 5 6 <🎜>$europe = array("United States", "UK", "France", "Germany", "Italy", "Spain", "Denmark"); <🎜> <🎜>if (in_array("United States",$europe)) {<🎜> <🎜>echo "True";<🎜> <🎜>}<🎜> <🎜>?>
Same as above, the execution result is True. Example 3: Strict type checking example ?
1 2 3 4 5 6 7 8 9 <🎜>$a = array('1.10', 12.4, 1.13);<🎜> <🎜>if (in_array('12.4', $a, true)) {<🎜> <🎜>echo "'12.4' found with strict check ";<🎜> <🎜>}<🎜> <🎜>if (in_array(1.13, $a, true)) {<🎜> <🎜>echo "1.13 found with strict check ";<🎜> <🎜>}<🎜> <🎜>?>

The output result is:

1.13 found with strict check

Example 4: Applying arrays within arrays

?

1

2

3

4

5

6

7

8

9

10

11

12

$a = array(array('p', 'h'), array('p', 'r'), 'o');

if (in_array(array('p', 'h'), $a)) {

echo "'ph' was found ";

}

if (in_array(array('f', 'i'), $a)) {

echo "'fi' was found ";

}

if (in_array('o', $a)) {

echo "'o' was found ";

}

?>

1 2

3

4

5

6

7

8

1

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

9 10

11

12

$a = array(array('p', 'h'), array('p', 'r'), 'o');

if (in_array(array('p', 'h'), $a)) {

} if (in_array(array('f', 'i'), $a)) { echo "'fi' was found "; } if (in_array('o', $a)) {
echo "'o' was found ";

}
?>
The output result is: 'ph' was found 'o' was found The specific usage is as follows: ?
1 bool in_array(mixed $needle,array $haystack [, bool $strict = FALSE ])
Search for needle in haystack, using relaxed comparison if strict is not set. Note: since php5.4. The array definition is changed from array() to array[]. I hope this article will be helpful to everyone’s PHP programming design. http://www.bkjia.com/PHPjc/973922.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/973922.htmlTechArticlePHP uses the in_array function to check whether a certain value exists in the array. This article mainly introduces PHP's use of the in_array function to check the array. Whether there is a certain value in, a more detailed analysis of the in_array function...
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