This article mainly introduces how to use the PHP function in_array (), and provides examples of common use, use of the third parameter, clone object, and multi-dimensional array, you can refer to a system function is_array () in PHP to determine whether a value is in an array.
Syntax:
The code is as follows:
In_array (value, array, type)
Return boolen
Parameter description:
Value: The value to be searched.
Array: the searched array.
Type: type, true, true, and false (default)
Example 1: normal use
Code:
The code is as follows:
$ Str = 1;
$ Arr = array (1, 3, 5, 7, 9 );
$ Boolvalue = in_array ($ str, $ arr );
Var_dump ($ boolvalue );
Execution result:
The code is as follows:
Bool (true)
Example 2: Use the third parameter
Incomplete
Code:
The code is as follows:
$ Str = '1 ';
$ Arr = array (1, 3, 5, 7, 9 );
$ Boolvalue = in_array ($ str, $ arr, false );
Var_dump ($ boolvalue );
Execution result:
The code is as follows:
Bool (true)
Full
Code:
The code is as follows:
$ Str = '1 ';
$ Arr = array (1, 3, 5, 7, 9 );
$ Boolvalue = in_array ($ str, $ arr, true );
Var_dump ($ boolvalue );
Execution result:
The code is as follows:
Bool (false)
Instance 3: Clone object
Code:
The code is as follows:
Class {
Public $ a = 1;
Public function fun (){
Return $ this->;
}
}
Class B {
Public $ a = 2;
Public function fun (){
Return $ this->;
}
}
$ A = new ();
$ B = new B ();
$ C = clone $;
$ Arr = array ($ a, $ B );
$ Boolvalue = in_array ($ c, $ arr, false );
Var_dump ($ boolvalue );
Execution result:
The code is as follows:
Bool (true)
Code:
The code is as follows:
Class {
Public $ a = 1;
Public function fun (){
Return $ this->;
}
}
Class B {
Public $ a = 2;
Public function fun (){
Return $ this->;
}
}
$ A = new ();
$ B = new B ();
$ C = clone $;
$ Arr = array ($ a, $ B );
$ Boolvalue = in_array ($ c, $ arr, true );
Var_dump ($ boolvalue );
Execution result:
The code is as follows:
Bool (false)
Example 4: Multi-dimensional array
Code:
The 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:
The code is as follows:
Bool (true)
Code:
The 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:
The code is as follows:
Bool (false)