views:

274

answers:

5

Hi all, I was wondering if anyone could help me out, I'm trying to find a script that will check my entire array and remove any duplicates if required, then spit out the array in the same format.

Here's an example of my array (as you will see there are some duplicates):

Array
(
    [0] => Array
        (
            [0] => stdClass Object
                (
                    [bid] => 34
                    [name] => Adrianos Pizza & Pasta
                    [imageurl] => sp_adrian.gif
                    [clickurl] => #
                )

            [1] => stdClass Object
                (
                    [bid] => 42
                    [name] => Ray White Mordialloc
                    [imageurl] => sp_raywhite.gif
                    [clickurl] => http://www.raywhite.com/
                )

            [2] => stdClass Object
                (
                    [bid] => 48
                    [name] => Beachside Osteo
                    [imageurl] => sp_beachside.gif
                    [clickurl] => http://www.beachsideosteo.com.au/
                )

            [3] => stdClass Object
                (
                    [bid] => 57
                    [name] => Southern Suburbs Physiotherapy Centre
                    [imageurl] => sp_sspc.jpg
                    [clickurl] => http://www.sspc.com.au
                )

            [4] => stdClass Object
                (
                    [bid] => 52
                    [name] => Mordialloc Travel and Cruise
                    [imageurl] => sp_morditravel.jpg
                    [clickurl] => http://www.yellowpages.com.au/vic/mordialloc/mordialloc-travel-cruise-13492525-listing.html
                )

            [5] => stdClass Object
                (
                    [bid] => 37
                    [name] => Mordialloc Cellar Door
                    [imageurl] => sp_cellardoor.gif
                    [clickurl] => 
                )

            [6] => stdClass Object
                (
                    [bid] => 53
                    [name] => Carmotive
                    [imageurl] => sp_carmotive.jpg
                    [clickurl] => http://www.carmotive.com.au/
                )

        )

    [1] => Array
        (
            [0] => stdClass Object
                (
                    [bid] => 55
                    [name] => 360South
                    [imageurl] => sp_360.jpg
                    [clickurl] => 
                )

            [1] => stdClass Object
                (
                    [bid] => 40
                    [name] => Ripponlea Mitsubishi
                    [imageurl] => sp_mitsubishi.gif
                    [clickurl] => 
                )

            [2] => stdClass Object
                (
                    [bid] => 57
                    [name] => Southern Suburbs Physiotherapy Centre
                    [imageurl] => sp_sspc.jpg
                    [clickurl] => http://www.sspc.com.au
                )

            [3] => stdClass Object
                (
                    [bid] => 52
                    [name] => Mordialloc Travel and Cruise
                    [imageurl] => sp_morditravel.jpg
                    [clickurl] => http://www.yellowpages.com.au/vic/mordialloc/mordialloc-travel-cruise-13492525-listing.html
                )

            [4] => stdClass Object
                (
                    [bid] => 37
                    [name] => Mordialloc Cellar Door
                    [imageurl] => sp_cellardoor.gif
                    [clickurl] => 
                )

            [5] => stdClass Object
                (
                    [bid] => 53
                    [name] => Carmotive
                    [imageurl] => sp_carmotive.jpg
                    [clickurl] => http://www.carmotive.com.au/
                )

        )

    [2] => Array
        (
            [0] => stdClass Object
                (
                    [bid] => 44
                    [name] => Mordialloc Personal Trainers
                    [imageurl] => sp_mordipt.gif
                    [clickurl] => #
                )

            [1] => stdClass Object
                (
                    [bid] => 36
                    [name] => Big River
                    [imageurl] => sp_bigriver.gif
                    [clickurl] => 
                )

            [2] => stdClass Object
                (
                    [bid] => 52
                    [name] => Mordialloc Travel and Cruise
                    [imageurl] => sp_morditravel.jpg
                    [clickurl] => http://www.yellowpages.com.au/vic/mordialloc/mordialloc-travel-cruise-13492525-listing.html
                )

            [3] => stdClass Object
                (
                    [bid] => 37
                    [name] => Mordialloc Cellar Door
                    [imageurl] => sp_cellardoor.gif
                    [clickurl] => 
                )

            [4] => stdClass Object
                (
                    [bid] => 53
                    [name] => Carmotive
                    [imageurl] => sp_carmotive.jpg
                    [clickurl] => http://www.carmotive.com.au/
                )

        )

    [3] => Array
        (
            [0] => stdClass Object
                (
                    [bid] => 41
                    [name] => Print House Graphics
                    [imageurl] => sp_printhouse.gif
                    [clickurl] => 
                )

            [1] => stdClass Object
                (
                    [bid] => 49
                    [name] => Kim Reed Conveyancing
                    [imageurl] => sp_kimreed.jpg
                    [clickurl] => 
                )

            [2] => stdClass Object
                (
                    [bid] => 37
                    [name] => Mordialloc Cellar Door
                    [imageurl] => sp_cellardoor.gif
                    [clickurl] => 
                )

            [3] => stdClass Object
                (
                    [bid] => 53
                    [name] => Carmotive
                    [imageurl] => sp_carmotive.jpg
                    [clickurl] => http://www.carmotive.com.au/
                )

        )

    [4] => Array
        (
            [0] => stdClass Object
                (
                    [bid] => 38
                    [name] => Lowe Financial Group
                    [imageurl] => sp_lowe.gif
                    [clickurl] => http://lowefinancial.com/
                )

            [1] => stdClass Object
                (
                    [bid] => 58
                    [name] => Dicount Lollie Shop
                    [imageurl] => new dls logo.jpg
                    [clickurl] => 
                )

            [2] => stdClass Object
                (
                    [bid] => 53
                    [name] => Carmotive
                    [imageurl] => sp_carmotive.jpg
                    [clickurl] => http://www.carmotive.com.au/
                )

        )

    [5] => Array
        (
            [0] => stdClass Object
                (
                    [bid] => 45
                    [name] => Mordialloc Sporting Club
                    [imageurl] => msc logo.jpg
                    [clickurl] => 
                )

            [1] => stdClass Object
                (
                    [bid] => 33
                    [name] => Two Brothers
                    [imageurl] => sp_2brothers.gif
                    [clickurl] => http://www.2brothers.com.au/
                )

        )

    [6] => Array
        (
            [0] => stdClass Object
                (
                    [bid] => 46
                    [name] => Patterson Securities
                    [imageurl] => cmyk patersons_withtag.jpg
                    [clickurl] => 
                )

            [1] => stdClass Object
                (
                    [bid] => 56
                    [name] => Logical Services
                    [imageurl] => sp_logical.jpg
                    [clickurl] => 
                )

        )

    [7] => Array
        (
            [0] => stdClass Object
                (
                    [bid] => 59
                    [name] => Pure Sport
                    [imageurl] => sp_psport.jpg
                    [clickurl] => http://www.puresport.com.au/
                )

            [1] => stdClass Object
                (
                    [bid] => 51
                    [name] => Richmond and Bennison
                    [imageurl] => sp_richmond.jpg
                    [clickurl] => http://www.richbenn.com.au/
                )

        )

    [8] => Array
        (
            [0] => stdClass Object
                (
                    [bid] => 39
                    [name] => Main Street Mordialloc
                    [imageurl] => main street cafe.jpg
                    [clickurl] => 
                )

            [1] => stdClass Object
                (
                    [bid] => 50
                    [name] => Letec
                    [imageurl] => sp_letec.jpg
                    [clickurl] => www.letec.biz
                )

        )

    [9] => Array
        (
            [0] => stdClass Object
                (
                    [bid] => 54
                    [name] => PPM Builders
                    [imageurl] => sp_ppm.jpg
                    [clickurl] => http://www.hotfrog.com.au/Companies/P-P-M-Builders
                )

            [1] => stdClass Object
                (
                    [bid] => 43
                    [name] => Systema
                    [imageurl] => sp_systema.gif
                    [clickurl] => 
                )

        )

)
+1  A: 

You can remove duplicates from an array by doing:

$array = array_values(array_unique($array));
Gazler
Does this work for multidimensional arrays? I don't think so...
Felix Kling
+1  A: 

Use the array_unique function.

Here is an implentation of a multi-dimensional array_unique function.

function super_unique($array)
{
  $result = array_map("unserialize", array_unique(array_map("serialize", $array)));

  foreach ($result as $key => $value)
  {
    if ( is_array($value) )
    {
      $result[$key] = super_unique($value);
    }
  }

  return $result;
}

Not tested, from the comments in the function manual.

Boris Guéry
I tried that, but it's still showing duplicates
SoulieBaby
+1, however that seems awfully familiar (http://stackoverflow.com/questions/1247950/how-to-remove-duplicated-2-dimension-array-in-php/1248189#1248189)... xP
Alix Axel
You need to cast the objects to arrays and use `array_merge`.
Alix Axel
Tried the code from the other thread but doesn't work with my array, it still comes up with duplicates..
SoulieBaby
+1  A: 

Not a direct answer, but can´t you catch it at query level as it seems it´s coming from a database?

Something like GROUP BY bid...

jeroen
I'm pulling the information from the database and splitting the single array into smaller arrays, but somehow it's getting duplicated, I'm guessing my array_split function is causing this..
SoulieBaby
So then fix that code instead of putting a band aid on it. I hope you're not getting paid for this, and I hope never to have to maintain anything you write.
George Jempty
I haven't seen your array_split function, but it seems to me you'd better fix that instead of patching things up afterwards.
jeroen
George, bad day?
jeroen
Actually the array_split function wasn't mine. I just have to deal with the consequences of that function, but thanks for your "enlightened" comment George.
SoulieBaby
A: 

My PHP's rusty, but something like this should work:

function makeUniqueBidArray(&$array)
{
  $tempArray = array();
  foreach($array as $bidArray) {
    foreach($bidArray as $bid) {
      if (isset($tempArray[$bid->bid]) {
        unset($bid);
      } else {
        $tempArray[$bid->bid] = $bid->name;
      }
    }
  }
}
Marcus Adams
A: 

stop using your own array splitting function that causes duplicates and read the php manual page on array_chunk

Kris