Skip to main content
Tweeted twitter.com/#!/StackCodeReview/status/235524678125506560
added 953 characters in body
Source Link
Simon Verbeke
  • 411
  • 1
  • 4
  • 12

EDIT 2:

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

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

[1] => Array
    (
        [0] => 10
        [1] => 9
        [2] => 8
        [3] => 7
    )

[2] => Array
    (
    )

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

    Array //This one uses the node ids from the previous array as keys, the values are the number of occurences.
    (
        [12] => 1
        [11] => 2
        [10] => 3
        [9] => 3
        [8] => 3
        [7] => 3
    )

EDIT 2:

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

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

[1] => Array
    (
        [0] => 10
        [1] => 9
        [2] => 8
        [3] => 7
    )

[2] => Array
    (
    )

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

    Array //This one uses the node ids from the previous array as keys, the values are the number of occurences.
    (
        [12] => 1
        [11] => 2
        [10] => 3
        [9] => 3
        [8] => 3
        [7] => 3
    )
added 328 characters in body
Source Link
Simon Verbeke
  • 411
  • 1
  • 4
  • 12

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 < count($nodes); $i++)
{
    for ($j = 0; $j < 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

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 < count($nodes); $i++)
{
    for ($j = 0; $j < 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!

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 < count($nodes); $i++)
{
    for ($j = 0; $j < 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

Source Link
Simon Verbeke
  • 411
  • 1
  • 4
  • 12

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 < count($nodes); $i++)
{
    for ($j = 0; $j < 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!