Remove Duplicates from Array

Hi all, I’m getting distinct sizes from my database which is working. I’m printing the array and it’s got a load of duplicate sizes in. I want to remove these. I’ve got a feeling I need to use array_unique but I can’t seem to implement, not sure where it needs to go. Any help would be appreciated as always :slight_smile:

<?php
$sizes = array();
	$position = 0;
		while($sig = mysql_fetch_array($sizesQuery)){
			$productSize = explode("\
", $sig['sizes']);
			foreach ($productSize as $id=>$item){
			$sizes[] = $item;
			}
		}
		print_r($sizes);
?>

If you’re really getting distinct sizes from the database, then why does the array have duplicates? I suspect you aren’t really getting distinct sizes from the database. Could you show your SQL query?

Hi Salathe, sure things here is my SQL query…

$sizesQuery = mysql_query("SELECT DISTINCT sizes FROM products AS p LEFT JOIN products_to_section as o ON p.id=o.prod_id WHERE live='1'");

Right ok, bit of an update, here is my code. When I print_r my array it shows the sizes I want. However, where I’m echoing out the array, it’s only displaying two so why isn’t the third element of the array echoing out?

<?php
$sizes = array();
$position = 0;
while($sig = mysql_fetch_array($sizesQuery)){
$productSize = explode("\
", $sig['sizes']);
foreach ($productSize as $id=>$item){
$sizes[] = $item;
echo $sizes[$id];
echo "<br/>";
}
}
print_r($sizes);
//Array ( [0] => [1] => 0-3 Months [2] => 3-6 Months [3] => 6-9 Months ) 
?>

Hey.

I’m a little confused, but here’s what I think you’re trying to do.


$sizes = array();

while($row = mysql_fetch_assoc($res)){
  foreach(explode("\
", $row['sizes']) as $size){
    array_push($sizes, $size);
    echo $size, '<br />';
  }
}

Hey bud, that’s just what I was after, it’s all working like a charm now! :slight_smile: Cheers bud!