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?

share|improve this question

1 Answer

up vote 0 down vote accepted

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);
share|improve this answer
Thanks, that worked! – Miguel Suarez Aug 30 '12 at 14:17

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.