php function In_array () use a detailed _php instance

Source: Internet
Author: User
PHP has a system function Is_array () to determine whether a value is in an array.
The syntax is as follows:
Copy CodeThe code is as follows:
In_array (Value,array,type)
Return Boolen

Parameter description:
Value: Values to search for
Array: the arrays being searched
Type: Types, True congruent, false non-congruent (default)

Example one: normal use

Code:
Copy CodeThe code is as follows:
$STR = 1;

$arr = Array (1,3,5,7,9);

$boolvalue = In_array ($str, $arr);

Var_dump ($boolvalue);

Execution Result:
Copy CodeThe code is as follows:
BOOL (TRUE)

example two: using a third parameter
Non-congruent
Code:
Copy CodeThe code is as follows:
$str = ' 1 ';

$arr = Array (1,3,5,7,9);

$boolvalue = In_array ($str, $arr, false);

Var_dump ($boolvalue);

Execution Result:
Copy CodeThe code is as follows:
BOOL (TRUE)

Congruent
Code:
Copy CodeThe code is as follows:
$str = ' 1 ';

$arr = Array (1,3,5,7,9);

$boolvalue = In_array ($str, $arr, true);

Var_dump ($boolvalue);

Execution Result:
Copy CodeThe code is as follows:
BOOL (FALSE)

example three: Cloning an object
Code:
Copy CodeThe code is as follows:
Class A {
public $a = 1;
Public Function Fun () {
return $this->a;
}
}

Class B {
Public $a = 2;
Public Function Fun () {
return $this->a;
}
}

$a = new A ();
$b = new B ();

$c = Clone $a;

$arr = Array ($a, $b);

$boolvalue = In_array ($c, $arr, false);

Var_dump ($boolvalue);

Execution Result:
Copy CodeThe code is as follows:
BOOL (TRUE)


Code:
Copy CodeThe code is as follows:
Class A {
public $a = 1;
Public Function Fun () {
return $this->a;
}
}

Class B {
Public $a = 2;
Public Function Fun () {
return $this->a;
}
}

$a = new A ();
$b = new B ();

$c = Clone $a;

$arr = Array ($a, $b);

$boolvalue = In_array ($c, $arr, true);

Var_dump ($boolvalue);

Execution Result:
Copy CodeThe code is as follows:
BOOL (FALSE)

Example four: multidimensional arrays
Code:
Copy CodeThe code is as follows:
$str = 10;

$arr = Array (
Array (1,2,3,4),
Array (5,6,7,8,9),
10
);

$boolvalue = In_array ($str, $arr);

Var_dump ($boolvalue);

Execution Result:
Copy CodeThe code is as follows: BOOL (TRUE)

Code:
Copy CodeThe code is as follows:
$str = 10;

$arr = Array (
Array (1,2,3,4),
Array (5,6,7,8,9,10),
);

$boolvalue = In_array ($str, $arr);

Var_dump ($boolvalue);
Execution Result:
Copy CodeThe code is as follows:
BOOL (FALSE)

  • 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.