PHP array intersection judgment and Optimizer code _php tutorial

Source: Internet
Author: User
Yesterday I have a function is need to judge the generation of multiple array intersection, that is, to determine whether the intersection of these arrays, I would like to introduce to you the PHP array intersection judgment program code example, there is a need for friends to reference.

Need to determine whether two arrays have intersection, the first feeling PHP should have this function, sure enough:

Array array_intersect (array Array1,array array2[,arrayn ...])

Returns the intersection element in the n array, and if it is associative, it can be used ARRAY_INTERSECT_ASSOC ()

The PHP case is as follows:

intersection of Arrays Array_intersect ()
The Array_intersect () function returns an array that retains the key, which consists only of the values that appear in the first array and that appear in each of the other input arrays. The form is as follows:

The code is as follows Copy Code


$fruit 1 = Array ("Apple", "Banana", "Orange");
$fruit 2 = Array ("Pear", "Apple", "Grape");
$fruit 3 = Array ("Watermelon", "Orange", "Apple");
$intersection = Array_intersect ($fruit 1, $fruit 2, $fruit 3);
Print_r ($intersection);
Output Array ([0] = Apple)
?>

My application is as follows:

The code is as follows Copy Code

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 associative arrays ARRAY_INTERSECT_ASSOC ()

The code is as follows Copy Code

$fruit 1 = Array ("Red" = "Apple", "yellow" = "Banana", "orange" and "Orange");
$fruit 2 = Array ("Yellow" = "Pear", "red" = "Apple", "Purple" and "Grape");
$fruit 3 = Array ("green" = "watermelon", "orange" and "orange", "Red" and "Apple");
$intersection = Array_intersect_assoc ($fruit 1, $fruit 2, $fruit 3);
Print_r ($intersection);

Output
Array ([red] = Apple)
?>

Optimization of array intersection

Assume that each parameter contains about 1000 product IDs (int), which is the premise to simulate generating some data:

The code is as follows Copy Code

$rand = function () {
$result = Array ();

for ($i = 0; $i < $i + +) {
$result [] = Mt_rand (1, 10000);
}

return $result;
};

$param _a = $rand ();
$param _b = $rand ();

?>

Note: If the test data set is too small, the conclusion may appear inconsistent.

Let's look at the performance achieved with PHP's built-in method Array_intersect:

The code is as follows Copy Code

$time = Microtime (true);

$result = Array_intersect ($param _a, $param _b);

$time = Microtime (True)-$time;

echo "Array_intersect: {$time}n";

?>

Before we optimize, let's take a look at some special areas of Array_intersect:

The code is as follows Copy Code

$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, if there are repeating elements in the first array argument, then Array_intersect returns all the repeating elements that satisfy the condition. It is best to be compatible with these features when rewriting array_intersect.

Here's a look at the performance achieved through the custom method Int_array_intersect:

The code is as follows Copy Code

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 ($a);
$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 would certainly think that built-in functions are faster than custom functions, but in this case the results are exactly the opposite:

array_intersect:0.023918151855469
int_array_intersect:0.0026049613952637


http://www.bkjia.com/PHPjc/445278.html www.bkjia.com true http://www.bkjia.com/PHPjc/445278.html techarticle yesterday I had a function is need to judge the generation of multiple array intersection, that is, to determine whether the intersection of these arrays, I would like to give you the students to introduce the PHP array intersection judgment process ...

  • 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.