Use of the PHP function in_array () _ php instance

Source: Internet
Author: User
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)

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.