Skip to main content
Inspiring
June 26, 2006
Question

PHP array_unique function not working

  • June 26, 2006
  • 1 reply
  • 843 views
hi:

i have a table in a MySQL DB containing a field named E_YEAR, type
year(4). i loop through all records storing years in an array:

$e_years = array();
...
(loop)
$e_years[$i] = $row_rsExhib['E_YEAR'];
...
(end loop)


as E_YEAR field has duplicated values i've used the array_unique
function to show only unique ones but no way it do the job, just shows
the first although the array has 4 values afeter array_unique

echo count($e_years).'-';
$e_years = array_unique($e_years);
echo count($e_years).'-';
for ($i=0; $i<count($e_years); $i++) {
echo $e_years[$i].'-';
}

output: 20-4-2003----

if i comment the second line this is the output;

20-20-2003-2003-2003-2003-2003-2004-2004-2004-2004-2004-2004-2005-2005-2005-2005-2005-2006-2006-2006-2006

surely i'm missing something obvious but can't find what it is

tia,

jdoe
This topic has been closed for replies.

1 reply

Inspiring
June 27, 2006
forced by the time i had to use a nasty workaround:

$y = implode(",", array_unique($e_years));
$e_years = explode(",", $y);
for ($i=0; $i<count($e_years); $i++) {
echo '<DIV class="years">'.$e_years[$i].'</DIV>';
}

someone can bring this to light?

tia,

jdoe


John Doe wrote:
> hi:
>
> i have a table in a MySQL DB containing a field named E_YEAR, type
> year(4). i loop through all records storing years in an array:
>
> $e_years = array();
> ...
> (loop)
> $e_years[$i] = $row_rsExhib['E_YEAR'];
> ...
> (end loop)
>
>
> as E_YEAR field has duplicated values i've used the array_unique
> function to show only unique ones but no way it do the job, just shows
> the first although the array has 4 values afeter array_unique
>
> echo count($e_years).'-';
> $e_years = array_unique($e_years);
> echo count($e_years).'-';
> for ($i=0; $i<count($e_years); $i++) {
> echo $e_years[$i].'-';
> }
>
> output: 20-4-2003----
>
> if i comment the second line this is the output;
>
> 20-20-2003-2003-2003-2003-2003-2004-2004-2004-2004-2004-2004-2005-2005-2005-2005-2005-2006-2006-2006-2006
>
>
> surely i'm missing something obvious but can't find what it is
>
> tia,
>
> jdoe