PHP In_array function Use instructions and in_array need to note the local instructions

Source: Internet
Author: User
In_array
(PHP 4, PHP 5)

in_array-checks if a value exists in the array

Description

BOOL In_array (mixed $needle, array $haystack [, BOOL $strict])

Searches for needle in haystack and returns TRUE if found, otherwise FALSE.

If the value of the third parameter strict is TRUE then the In_array () function also checks whether the needle type is the same as in haystack.

Note: If needle is a string, the comparison is case-sensitive.

Note: Needle is not allowed to be an array until PHP version 4.2.0.

Example #1 In_array () example

<?php $os = Array ("Mac", "NT", "Irix", "Linux"); if (In_array ("Irix", $os)) {echo "Got Irix";} if (In_array ("Mac", $os)) {echo "Got mac";}?>

The second condition fails because In_array () is case-sensitive, so the above program appears as:
Got Irix

Example #2 In_array () strict type checking example

<?php $a = array (' 1.10 ', 12.4, 1.13); if (In_array (' 12.4 ', $a, true)) {echo "' 12.4 ' found with strict check\n";} if (In_array (1.13, $a, true)) {echo "1.13 fo und with strict check\n "; }?>

The example above will output:

1.13 Found with strict check

Example #3 In_array () using Arrays as needle

<?php $a = array (' P ', ' h '), Array (' P ', ' r '), ' o '); if (In_array (' P ', ' h '), $a) {echo "' ph ' was found\n";} if (In_array (' f ', ' I '), $a) {echo "' fi ' is found\ n "; } if (In_array (' o ', $a)) {echo "' O ' was found\n";}?>

The example above will output:

' ph ' was found
' O ' was found

Places to be aware of:

If:

First declare an array as:

$arr = Array (*);

Then there are:

In_array (0, $arr) = = True

Puzzling! {Weak language}


Workaround:
In_array (strval (0), $arr, True))


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.