如果值存在于所有数组中,如何在PHP中从多维数组返回重复值


How to return duplicate values from multidimentional array in PHP if the value exists in all arrays.

我已经想了好几天了,但没有得到好的结果。我在PHP中有一个多维度的对象数组(print_r):

Array
(
[3] => Array
    (
        [0] => stdClass Object
            (
                [productoId] => 16
            )
        [1] => stdClass Object
            (
                [productoId] => 21
            )
        [2] => stdClass Object
            (
                [productoId] => 22
            )
    )
[7] => Array
    (
        [0] => stdClass Object
            (
                [productoId] => 16
            )
        [1] => stdClass Object
            (
                [productoId] => 21
            )
        [2] => stdClass Object
            (
                [productoId] => 22
            )
    )
[6] => Array
    (
        [0] => stdClass Object
            (
                [productoId] => 16
            )
        [1] => stdClass Object
            (
                [productoId] => 17
            )
    )
)

我想得到所有数组中存在的重复值,例如:

stdClass Object
        (
            [productoId] => 16
        )

但不是:

stdClass Object
        (
            [productoId] => 21
        )

你知道我怎样才能做到这一点吗?

如下:

$array = array(
            3 => array((object) array('productoId' => '16'), (object) array('productoId' => '21'), (object) array('productoId' => '22')),
            7 => array((object) array('productoId' => '16'), (object) array('productoId' => '21'), (object) array('productoId' => '22')),
            6 => array((object) array('productoId' => '16'), (object) array('productoId' => '17'))
        );
$arrays = count($array);
$match = array();
$duplicates = array();
foreach($array as $one){
    foreach($one as $single){
        $var = (array)$single;
        if(!isset($match[$var['productoId']])) { $match[$var['productoId']] = 0; }
        $match[$var['productoId']]++;
        if($match[$var['productoId']] == $arrays){
            $duplicates[] = (int)$var['productoId'];
        }
    }
}
print_r($duplicates);