Get unique entries from array For a website I'm working on, I had to get all the unique entries in an array and count their occurrence. It is possible a certain entry is only found once, but it can also be found 20 times. So I designed the following bit of code:

for ($i = 0; $i &lt; count($nodes); $i++)
{
    for ($j = 0; $j &lt; count($nodes[$i]); $j++)
    {
        if (!array_key_exists($nodes[$i][$j], $uniekenodes))
        {
            $uniekenodes[$nodes[$i][$j]] = 1;
        }
        else
        {
            $uniekenodes[$nodes[$i][$j]] += 1;
        }
    }
}


The $nodes array contains the the entries returned from the database. And the $uniekenodes array contains the unique entries and how many times they occured in the $nodes array.

This is my first php script (on a drupal webpage by the way) and as such I don't know that much about php. I'm pretty confident there is probably a more a efficient way to do this, using php-specific functions. Any and all tips are welcome!

EDIT: I might have to clarify the structure of the arrays:

$nodes has two dimensions. The first dimension is just a key for the second dimension. This one contains an array of drupal nodes for each key.

$uniekenodes uses the nodes from $nodes as a key and the value is how many times the node occured in $nodes

EDIT 2:

I printed the arrays, as requested by Boris Guéry:

Array
(    
[0] =&gt; Array //Each of these contains node id's returned by a query
    (
        [0] =&gt; 12
        [1] =&gt; 11
        [2] =&gt; 10
        [3] =&gt; 9
        [4] =&gt; 8
        [5] =&gt; 7
    )

[1] =&gt; Array
    (
        [0] =&gt; 10
        [1] =&gt; 9
        [2] =&gt; 8
        [3] =&gt; 7
    )

[2] =&gt; Array
    (
    )

[3] =&gt; Array
    (
        [0] =&gt; 11
        [1] =&gt; 10
        [2] =&gt; 9
        [3] =&gt; 8
        [4] =&gt; 7
       )    
)

    Array //This one uses the node ids from the previous array as keys, the values are the number of occurences.
    (
        [12] =&gt; 1
        [11] =&gt; 2
        [10] =&gt; 3
        [9] =&gt; 3
        [8] =&gt; 3
        [7] =&gt; 3
    )
I think the associative array approach is good. You could probably get away with:

for ($i = 0; $i &lt; count($nodes); $i++)
  for ($j = 0; $j &lt; count($nodes[$i]); $j++)
    $uniekenodes[$nodes[$i][$j]]++;


As incrementing NULL values results in 1.