Php array intersection judgment and optimization program code _ PHP Tutorial

Source: Internet
Author: User
Php array intersection judgment and optimization program code. Yesterday, I had a function to determine the intersection of multiple generated arrays, that is, to determine whether there is an intersection in these arrays, next I will introduce to you the php array intersection judgment process. yesterday I had a function to determine the intersection of multiple generated arrays, that is, to determine whether there is an intersection in these arrays, next I will introduce you to php array intersection judgment program code examples. if you need it, please refer.

You need to determine whether the two arrays have an intersection. The first one feels that PHP should have this function. sure enough:

Array array_intersect (array array1, array array2 [, arrayN…])

Returns the intersection element of N arrays. if it is an associated array, you can use array_intersect_assoc ()

The PHP case is as follows:

Array intersectionArray_intersect ()
The array_intersect () function returns an array with keys retained. this array is composed of only values that appear in the first array and appear in each other input array. The format is as follows:

The code is as follows:


$ Fruit1 = array ("Apple", "Banana", "Orange ");
$ Fruit2 = array ("Pear", "Apple", "Grape ");
$ Fruit3 = array ("Watermelon", "Orange", "Apple ");
$ Intersection = array_intersect ($ fruit1, $ fruit2, $ fruit3 );
Print_r ($ intersection );
// Output Array ([0] => Apple)
?>

My applications are as follows:

The code is as follows:

If ($ user-> role! = 1 ){
$ Count = count ($ projects );
For ($ I = 0; $ I <$ count; $ I ++ ){
If (! Array_intersect (explode (',', $ projects [$ I] ['role']), explode (',', $ projects [$ I] ['next _ approve_role ']) {
Unset ($ projects [$ I]);
Continue;
}
}
}

Intersection of correlated arraysArray_intersect_assoc ()

The code is as follows:

$ Fruit1 = array ("red" => "Apple", "yellow" => "Banana", "orange" => "Orange ");
$ Fruit2 = array ("yellow" => "Pear", "red" => "Apple", "purple" => "Grape ");
$ Fruit3 = array ("green" => "Watermelon", "orange" => "Orange", "red" => "Apple ");
$ Intersection = array_intersect_assoc ($ fruit1, $ fruit2, $ fruit3 );
Print_r ($ intersection );

// Output
// Array ([red] => Apple)
?>

Array intersection optimization

Assuming that each parameter contains about one thousand product IDs (int), the following data is generated on the premise that:

The code is as follows:

$ Rand = function (){
$ Result = array ();

For ($ I = 0; I I <1000; $ I ++ ){
$ Result [] = mt_rand (1, 10000 );
}

Return $ result;
};

$ Param_a = $ rand ();
$ Param_ B = $ rand ();

?>

Note: If the test dataset is too small, the conclusions may be inconsistent.

First, let's look at the performance achieved through the PHP built-in method array_intersect:

The code is as follows:

$ Time = microtime (true );

$ Result = array_intersect ($ param_a, $ param_ B );

$ Time = microtime (true)-$ time;

Echo "array_intersect: {$ time} n ";

?>

Before optimization, let's take a look at some special aspects of array_intersect:

The code is as follows:

$ Param_a = array (1, 2, 2 );
$ Param_ B = array (1, 2, 3 );

Var_dump (
Array_intersect ($ param_a, $ param_ B ),
Array_intersect ($ param_ B, $ param_a)
);

?>

Array_intersect ($ param_a, $ param_ B): 1, 2, 2
Array_intersect ($ param_ B, $ param_a): 1, 2
That is to say, if there are repeated elements in the first array parameter, array_intersect returns all repeated elements that meet the conditions. It is best to be compatible with these functions when rewriting array_intersect.

The following describes the performance achieved through the custom method int_array_intersect:

The code is as follows:

Function int_array_intersect ()
{
If (func_num_args () <2 ){
Trigger_error ('param error', E_USER_ERROR );
}

$ Args = func_get_args ();

Foreach ($ args AS $ arg ){
If (! Is_array ($ arg )){
Trigger_error ('param error', E_USER_ERROR );
}
}

$ Intersect = function ($ a, $ B ){
$ Result = array ();

$ Length_a = count ($ );
$ Length_ B = count ($ B );

For ($ I = 0, $ j = 0; $ I <$ length_a & $ j <$ length_ B; null ){
If ($ a [$ I] <$ B [$ j] & ++ $ I ){
Continue;
}

If ($ a [$ I]> $ B [$ j] & ++ $ j ){
Continue;
}

$ Result [] = $ a [$ I];

If (isset ($ a [$ next = $ I + 1]) & $ a [$ next]! = $ A [$ I]) {
+ + $ J;
}
++ $ I;
}

Return $ result;
};

$ Result = array_shift ($ args );
Sort ($ result );

Foreach ($ args as $ arg ){
Sort ($ arg );
$ Result = $ intersect ($ result, $ arg );
}

Return $ result;
}

$ Time = microtime (true );

$ Result = int_array_intersect ($ param_a, $ param_ B );

$ Time = microtime (true)-$ time;

Echo "int_array_intersect: {$ time} n ";

?>

Intuitively, we will certainly think that built-in functions are faster than custom functions, but the results in this example are the opposite:

Array_intersect: 0.023918151855469
Int_array_intersect: 0.0026049613952637


...

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.