Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 不同大小的相交多维数组_Php_Multidimensional Array - Fatal编程技术网

Php 不同大小的相交多维数组

Php 不同大小的相交多维数组,php,multidimensional-array,Php,Multidimensional Array,我有一个多维数组,如下所示: Array ( [0] => Array ( [0] => Array ( [id] => 3 ) [1] => Array ( [id] => 1 ) [2] => Array (

我有一个多维数组,如下所示:

Array
(
[0] => Array
    (
        [0] => Array
            (
                [id] => 3
            )

        [1] => Array
            (
                [id] => 1
            )

        [2] => Array
            (
                [id] => 2
            )

        [3] => Array
            (
                [id] => 5
            )

        [4] => Array
            (
                [id] => 4
            )

    )

[1] => Array
    (
        [0] => Array
            (
                [id] => 1
            )

        [1] => Array
            (
                [id] => 3
            )

        [2] => Array
            (
                [id] => 4
            )

        [3] => Array
            (
                [id] => 5
            )

    )

[2] => Array
    (
        [0] => Array
            (
                [id] => 3
            )

    )

)
我需要找到一种返回相交值的方法。在这种情况下,这将是

[id] => 3

数组的长度可能不同,因此我不能只使用array_intersect()。

如果数组只包含整数,这将很简单,但由于它们包含另一个数组,因此会变得更复杂。但这应该做到:

function custom_intersect($arrays) {
    $comp = array_shift($arrays);
    $values = array();

    // The other arrays are compared to the first array:
    // Get all the values from the first array for comparison
    foreach($comp as $k => $v) {
        // Set amount of matches for value to 1.
        $values[$v['id']] = 1;
    }

    // Loop through the other arrays
    foreach($arrays as $array) {
        // Loop through every value in array
        foreach($array as $k => $v) {
            // If the current ID exists in the compare array
            if(isset($values[$v['id']])) {
                // Increase the amount of matches
                $values[$v['id']]++;
            }
        }
    }

    $result = array();

    // The amount of matches for certain value must be
    // equal to the number of arrays passed, that's how
    // we know the value is present in all arrays.
    $n = count($arrays) + 1;
    foreach($values as $k => $v) {
        if($v == $n) {
            // The value was found in all arrays,
            // thus it's in the intersection
            $result[] = $v;
        }
    }
    return $result;
}
用法: 结果:
这个函数并不完美:如果在一个数组中有重复的ID,它将无法工作。这将需要更多的代码来首先使数组值唯一,但这在您的情况下可能会起作用。

如php.net网站(array\u intersect函数)的一条注释所述

这将返回此数组:

Array
(
    [0] => 1
    [1] => 2
    [2] => 4
    [3] => 5
    [4] => 6
)

您可以使用
array\u uintersect()
使用自定义比较函数获取数组的交集。您必须使用
call\u user\u func\u array()
调用它,因为它希望每个数组都作为一个单独的参数:

//build list of parameters for array_uintersect()
$params = array_merge($input, array('compare_func'));

$result = call_user_func_array('array_uintersect', $params);

function compare_func($a, $b) {
    return $a['id'] - $b['id'];   
}
你不能简单地用
call\u user\u func\u array()
调用
array\u intersect();
$b=数组(4,3,2,1);
$c=数组(1,2,3,4,5);
$d=数组(5,4,3);
$array=数组($a、$b、$c、$d);

对于($i=0;$i),不幸的是,在这种情况下它不起作用,因为数组包含设置了键“id”的数组。您的操作仅适用于具有标量值的数组-这需要一个自定义函数。
$a = array(1,2,3,4,5,2,6,1);  /* repeated elements --> $a is not a set */
$b = array(0,2,4,6,8,5,7,9,2,1);  /* repeated elements --> $b is not a set */

$ua = array_merge(array_unique($a));  /* now, $a is a set */
$ub = array_merge(array_unique($b));  /* now, $b is a set */

$intersect = array_merge(array_intersect($ua,$ub));
Array
(
    [0] => 1
    [1] => 2
    [2] => 4
    [3] => 5
    [4] => 6
)
//build list of parameters for array_uintersect()
$params = array_merge($input, array('compare_func'));

$result = call_user_func_array('array_uintersect', $params);

function compare_func($a, $b) {
    return $a['id'] - $b['id'];   
}
$a = array(4,3);
$b = array(4,3,2,1);
$c = array(1,2,3,4,5);
$d = array(5,4,3);

$array = array($a,$b,$c,$d);

for ($i=0; $i<count($array); $i++){
    if ($i==0){
        $array2 = $array[$i];
    } else {
        $array2 = array_intersect($array2, $array[$i]);
    }
}

print_r($array2);`