📜  PHP | array_intersect_uassoc()函数

📅  最后修改于: 2022-05-13 01:57:39.861000             🧑  作者: Mango

PHP | array_intersect_uassoc()函数

array_intersect_uassoc()函数是PHP的一个内置函数。它用于通过使用用户定义的比较函数来比较两个或多个数组的键和值并返回匹配项。
比较函数返回一个等于、大于或小于零的整数。如果第一个参数要分别考虑小于、等于或大于第二个。如果条件为真,则返回 TRUE 值,否则返回 FALSE 值。
句法:

使用的参数:此函数接受最少三个参数,所有三个参数都是必需的,其他参数是可选的。参数说明如下:

  1. $array1(必需):
    该数组将与其他数组进行比较。
  2. $array2(必需):
    该数组与第一个数组进行比较。
  3. $array3...(可选):
    该数组与第一个数组进行比较。
  4. uassoc_intersectFunction(必需):
    它是必需的用户定义函数。定义可调用比较函数的字符串。比较函数返回一个小于、等于或大于 0 的整数。如果第一个参数小于、等于或大于第二个参数。

返回值
返回一个数组,其中包含来自 array1 的条目,这些条目存在于所有其他数组中,例如:-(arra2, arra3, arar4....more)。返回值类型是一个数组。

注意:该函数使用用户定义的函数来比较。 (用户定义函数的
功能适用于键而不适用于键的值)



让我们通过一个例子来理解 array_intersect_uassoc()函数。
程序: 1在本例中,我们使用不区分大小写的 strcasecmp函数来比较数组的键。它比较不区分大小写的键。

php
 "gfg",
    "b" => "GeeksforGeeks",
    "c" => "contribute"
);
$arr2 = array(
    "a" => "gfg",
    "B" => "GeeksforGeeks",
    "c" => "ide"
);
$arr3 = array(
    "x" => "gfg",
    "B" => "GeeksforGeeks",
    "c" => "practice"
);
 
 
 
// Compare the keys and values by using a
// user-defined key comparison function.
// Here callback function applicable on keys
echo "Using function: array_intersect_uassoc() \n ";
$result = array_intersect_uassoc($arr1,
                $arr2, $arr3, "strcasecmp");
 
// printing result
print_r($result);
 
?>


PHP
 $arr2) ? 1 : -1;
}
 
// Code driven
$arr1 = array(
    "0" => "Graph",
    "1" => "Dynamic",
    "3" => "Recursive",
    "4" => "Prime Factor"
);
$arr2 = array(
    "4" => "Prime",
    "2" => "Factorial",
    "3" => "Recursive",
    "7" => "Modulo"
);
 
$result = array_intersect_uassoc($arr1,
        $arr2, "uassoc_intersectFunction");
print_r($result);
 
?>


PHP
 $arr2) ? 1 : -1;
}
 
// Code driven
$arr1 = array(
    "a" => "gfg",
    "b" => "GeeksforGeeks",
    "c" => "contribute"
);
$arr2 = array(
    "a" => "gfg",
    "B" => "GeeksforGeeks",
    "c" => "ide"
);
$arr3 = array(
    "a" => "Gfg",
    "B" => "GeeksforGeeks",
    "c" => "practice"
);
 
// userdefine function match as it is keys
// with same values but no any such
// case so it will return NULL
$result = array_intersect_uassoc(
$arr1, $arr2, $arr3, "uassoc_intersectFunction");
print_r($result);
 
?>


php

// array_intersect() function
// array_intersect_assoc() function
// array_uintersect_assoc() function
// array_intersect_key() function
 
$arr1 = array(
    "a" => "gfg",
    "b" => "GeeksforGeeks",
    "c" => "contribute"
);
$arr2 = array(
    "a" => "gfg",
    "B" => "GeeksforGeeks",
    "c" => "ide"
);
$arr3 = array(
    "a" => "Gfg",
    "B" => "GeeksforGeeks",
    "c" => "practice"
);
 
// The array_intersect() function compares
// the values (not keys) of two (or more)
// arrays, and returns the matches.
echo "**********array_intersect********** \n ";
$result = array_intersect($arr1, $arr2, $arr3);
print_r($result);
 
 
// array_intersect_assoc() returns an array
// containing all the values of arr1 that
// are present in all the arguments.
// for above input it will return null
echo "******array_intersect_assoc******** \n ";
$result = array_intersect_assoc($arr1, $arr2, $arr3);
print_r($result);
 
// array_uintersect_assoc compare values (data)
// by using call back function
echo "*********array_uintersect_assoc********** \n ";
$result = array_uintersect_assoc($arr1,
                    $arr2, $arr3, "strcasecmp");
print_r($result);
 
// Compare the keys and values by using
// a user-defined key comparison function
// here callback function applicable on keys
echo "*********array_uintersect_assoc *********\n ";
$result = array_intersect_uassoc($arr1,
                    $arr2, $arr3, "strcasecmp");
print_r($result);
 
// compute the intersection of two or more arrays.
// function returns another array containing
// the elements of the first array that
// are present in other arrays
echo "*********array_intersect_key *********\n ";
$result = array_intersect_key($arr1,
                    $arr2, $arr3);
print_r($result);
 
 
?>


输出:

Using function: array_uintersect_assoc() 
 Array
(
    [b] => GeeksforGeeks
)

程序: 2取两个数组(array1 和array2)并使用用户定义的键比较函数(uassoc_intersectFunction)。仅具有匹配键和值的函数返回数组。

PHP

 $arr2) ? 1 : -1;
}
 
// Code driven
$arr1 = array(
    "0" => "Graph",
    "1" => "Dynamic",
    "3" => "Recursive",
    "4" => "Prime Factor"
);
$arr2 = array(
    "4" => "Prime",
    "2" => "Factorial",
    "3" => "Recursive",
    "7" => "Modulo"
);
 
$result = array_intersect_uassoc($arr1,
        $arr2, "uassoc_intersectFunction");
print_r($result);
 
?>

输出:

Array
(
    [3] => Recursive
)



程序: 3取三个数组(arr1、arr2 和arr3)并使用用户定义的键比较函数(uassoc_intersectFunction)。用户定义函数匹配,因为它是具有相同值的键,但没有找到任何这样的情况,那么它将返回 NULL/空数组(在程序 1 中,我们使用 strcasecmp函数将此函数应用于键并忽略大小写的敏感性并返回结果 GeeksforGeeks。)

PHP

 $arr2) ? 1 : -1;
}
 
// Code driven
$arr1 = array(
    "a" => "gfg",
    "b" => "GeeksforGeeks",
    "c" => "contribute"
);
$arr2 = array(
    "a" => "gfg",
    "B" => "GeeksforGeeks",
    "c" => "ide"
);
$arr3 = array(
    "a" => "Gfg",
    "B" => "GeeksforGeeks",
    "c" => "practice"
);
 
// userdefine function match as it is keys
// with same values but no any such
// case so it will return NULL
$result = array_intersect_uassoc(
$arr1, $arr2, $arr3, "uassoc_intersectFunction");
print_r($result);
 
?>

输出:

Array
(
)

下面是一些相关的数组相交PHP函数

  • PHP | array_intersect()函数: 该函数返回另一个数组,其中包含第一个数组的元素,这些元素存在于作为参数传递的所有其他数组中。如果没有元素匹配,则返回 NULL 数组。
  • PHP | array_intersect_assoc()函数:函数返回第一个数组中所有其他参数中与第一个数组中索引相同的所有值,即键主要用于比较。
  • PHP | array_uintersect_assoc()函数:比较数组的键和用户定义的函数来比较值。
  • PHP | array_intersect_key()函数:该函数返回另一个数组,其中包含第一个数组的元素,这些元素存在于作为参数传递的其他数组中,这些数组的键相互匹配。如果没有键匹配,则返回一个 NULL 数组。

PHP


// array_intersect() function
// array_intersect_assoc() function
// array_uintersect_assoc() function
// array_intersect_key() function
 
$arr1 = array(
    "a" => "gfg",
    "b" => "GeeksforGeeks",
    "c" => "contribute"
);
$arr2 = array(
    "a" => "gfg",
    "B" => "GeeksforGeeks",
    "c" => "ide"
);
$arr3 = array(
    "a" => "Gfg",
    "B" => "GeeksforGeeks",
    "c" => "practice"
);
 
// The array_intersect() function compares
// the values (not keys) of two (or more)
// arrays, and returns the matches.
echo "**********array_intersect********** \n ";
$result = array_intersect($arr1, $arr2, $arr3);
print_r($result);
 
 
// array_intersect_assoc() returns an array
// containing all the values of arr1 that
// are present in all the arguments.
// for above input it will return null
echo "******array_intersect_assoc******** \n ";
$result = array_intersect_assoc($arr1, $arr2, $arr3);
print_r($result);
 
// array_uintersect_assoc compare values (data)
// by using call back function
echo "*********array_uintersect_assoc********** \n ";
$result = array_uintersect_assoc($arr1,
                    $arr2, $arr3, "strcasecmp");
print_r($result);
 
// Compare the keys and values by using
// a user-defined key comparison function
// here callback function applicable on keys
echo "*********array_uintersect_assoc *********\n ";
$result = array_intersect_uassoc($arr1,
                    $arr2, $arr3, "strcasecmp");
print_r($result);
 
// compute the intersection of two or more arrays.
// function returns another array containing
// the elements of the first array that
// are present in other arrays
echo "*********array_intersect_key *********\n ";
$result = array_intersect_key($arr1,
                    $arr2, $arr3);
print_r($result);
 
 
?>

输出:

**********array_intersect********** 
 Array
(
    [b] => GeeksforGeeks
)
******array_intersect_assoc******** 
 Array
(
)
*********array_uintersect_assoc********** 
 Array
(
    [a] => gfg
)
*********array_uintersect_assoc *********
 Array
(
    [b] => GeeksforGeeks
)
*********array_intersect_key *********
 Array
(
    [a] => gfg
     => contribute
)