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?

1
  • 4
    Have you tried $unique = array_unique($array, SORT_REGULAR);? Commented Apr 20, 2011 at 13:45

2 Answers 2

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.
Sign up to request clarification or add additional context in comments.

Comments

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);
?>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.