1

Ive been trying to figure this out for some days now without good results. I have this multidimentional array of objects in 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
            )

    )

)

I would like to get the duplicate values that exist in all the arrays, example:

stdClass Object
        (
            [productoId] => 16
        )

But not:

stdClass Object
        (
            [productoId] => 21
        )

Any idea how I can achieve this?

4

1 に答える 1

0

like this:

$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 に答える