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)