1

I have some array containing other arrays:

Array
(
    [0] => Slip Object
        (
            [userId:protected] => 1
            [parentSlipId:protected] => 0
            [id:protected] => 25
            [madeDatetime:protected] => 2011-04-19 17:13:09
            [stake:protected] => 34.00
            [status:protected] => 6
        )

    [1] => Slip Object
        (
            [userId:protected] => 1
            [parentSlipId:protected] => 0
            [id:protected] => 25
            [madeDatetime:protected] => 2011-04-19 17:13:09
            [stake:protected] => 34.00
            [status:protected] => 6
        )

    [2] => Slip Object
        (
            [userId:protected] => 1
            [parentSlipId:protected] => 0
            [id:protected] => 24
            [madeDatetime:protected] => 2011-04-18 11:31:26
            [stake:protected] => 13.00
            [status:protected] => 6
        )    
)

What's the best way of counting unique arrays?

akrisanov
  • 3,212
  • 6
  • 33
  • 56

2 Answers2

3

Off the top of my head you could try:

$hashes = array();
$uniques = 0;
foreach($array as $slip) {
    $hash = sha1(serialize($slip));
    if(!in_array($hash, $hashes)) {
        ++$uniques;
        $hashes[] = $hash;
    }
}
var_dump($uniques); // prints total number of unique objects.

Edit: @biakaveron's idea looks better though and could be adapted to:

$uniques = count(array_unique($array, SORT_REGULAR));
var_dump($uniques); // prints total number of unique objects.
Treffynnon
  • 21,365
  • 6
  • 65
  • 98
0

This previous question has various solutions for removing duplicate arrays from within an array. If you implement any of them and then use sizeof() on the returned array you will have your solution.

eg:

<?php
$yourarray = array();

$tmp = array ();

foreach ($yourarray as $row) 
    if (!in_array($row,$tmp)) array_push($tmp,$row);

echo sizeof($tmp);
?>
Community
  • 1
  • 1
kaykayman
  • 373
  • 2
  • 13