The in_array () function has 1st parameters as an array. is that all right?

Source: Internet
Author: User
The in_array () function has 1st parameters as an array. is that all right?
 111,2 => 222,3 => 333); $ x2 = array (1 => 777,2 => 111,3 => 222, 4 => 333) $ y = in_array ($ x1, $ x2 ); // The 1st parameters are arrays. Echo $ y; echo "\ r \ n" ;?>



Why are program errors?


Reply to discussion (solution)

The first parameter of in_array should be single-value data.

If it is an array, use array_intersect to obtain the intersection.

The first parameter of in_array should be single-value data.

If it is an array, use array_intersect to obtain the intersection.

Http://docs.php.net/manual/zh/function.in-array.php

Update Log

Version description
Before PHP version 4.2.0, needle cannot be an array.

PHP official Chinese document. what does the above mean?

Er, read the manual carefully.

It can be an array. The last example in the manual is.

The first parameter of in_array should be single-value data.

If it is an array, use array_intersect to obtain the intersection.


 


In a one-dimensional array, search and return: 1
Why, in a 3D array, search and return: 0?

Change:

In a two-dimensional array, search and return: 1
Why, in a 3D array, search and return: 0?

Change:

In a two-dimensional array, search and return: 1
Why, in a 3D array, search and return: 0?

Echo in_array (array ("1", "2"), "B"), array ("1", "2 "), "B"), "c "))? 'True': 'false ';

If you write the in_array function by yourself, you will know why ....

The first parameter of in_array should be single-value data.

If it is an array, use array_intersect to obtain the intersection.


When the first parameter is an array,
It can only be one dimension of a multi-dimensional array to be searched (that is, the first parameter), isn't it like this?

The comparison is only performed in the first dimension.
If the first parameter array is used, the connection structure is matched together.

$a = array(array("a","b"),"c");var_dump( in_array(array("a","b"), $a) ); //bool(true)var_dump( in_array(array("b","a"), $a) ); //bool(false)

Therefore, it is recommended that you do not use the data in this way. it is more appropriate to use only the leaflet value data.

The comparison is only performed in the first dimension.
If the first parameter array is used, the connection structure is matched together.

$a = array(array("a","b"),"c");var_dump( in_array(array("a","b"), $a) ); //bool(true)var_dump( in_array(array("b","a"), $a) ); //bool(false)

Therefore, it is recommended that you do not use the data in this way. it is more appropriate to use only the leaflet value data.

 


Why can't the result be found in the program? 0 is returned?

$ X2 = array (
Array ("1", "2"), "B "),
"C"
);
The first dimension is like this.
You cannot find array ("1", "2 ").

New users cannot understand

In special cases, it is possible.

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.