1

几天来,我一直试图解决这个问题,但没有得到好的结果。我在 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
        )

知道如何实现这一目标吗?

4

1 回答 1

0

像这样:

$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);
于 2012-08-30T00:45:40.647 回答