PDA

View Full Version : adding to an array on loop than remove all duplicates


gavimobile
02-19-2012, 06:10 PM
folks,
i am trying to add my database output on each loop to an array than output the results without duplicates.
Please point me in the right direction, im not very good with arrays or for loops. This is what i have now which doesnt work at all.

while ($row = mysql_fetch_assoc($result)) {
$month= $row['some_date'];
//create an array
$myArray = array();
//add the months to an array
$myArray = array_push($myArray, $month);
//search for duplicate months and remove them
$myArray = array_unique($myArray);
var_dump($myArray);
}
thanks in advance
gm

gavimobile
02-19-2012, 06:14 PM
my guess is that
$myArray = array();
needs to be before the loop, but im still missing something else.

edbr
02-20-2012, 05:17 AM
try this


$myArray = array();
array_push($myArray, $month);

gavimobile
02-20-2012, 07:43 AM
still doesn't seem to be working.
i see the output but there is still doubles

i tried
while ($row = mysql_fetch_assoc($result)) {
$month= $row['some_date'];
//create an array
$myArray = array();
//add the months to an array
array_push($myArray, $month);
//search for duplicate months and remove them
$myArray = array_unique($myArray);
var_dump($myArray);
}

and

while ($row = mysql_fetch_assoc($result)) {
$month= $row['some_date'];
//create an array
$myArray = array();
//add the months to an array
array_push($myArray, $month);
//search for duplicate months and remove them
array_unique($myArray);
var_dump($myArray);
}

and

while ($row = mysql_fetch_assoc($result)) {
$month= $row['some_date'];
//create an array
$myArray = array();
//add the months to an array

//search for duplicate months and remove them
array_unique(array_push($myArray, $month));
var_dump($myArray);
}

edbr
02-20-2012, 08:21 AM
second one looks correct but just read this, if it helps

some functions there
http://php.net/manual/en/function.array-unique.php

gavimobile
02-20-2012, 08:45 AM
thanks, i read the document before you even posted it however i still cant make this work for some reason. heres the output of my second example.

array(1) { [0]=> string(10) "November " } array(1) { [0]=> string(9) "November " } array(1) { [0]=> string(5) "May " } array(1) { [0]=> string(9) "October " } array(1) { [0]=> string(5) "June " } array(1) { [0]=> string(10) "September " } array(1) { [0]=> string(6) "July " }

note that november is listed twice


while ($row = mysql_fetch_assoc($result)) {
//create an array
$myArray = array();
//add the months to an array
array_push($myArray, $month);
//search for duplicate months and remove them
array_unique($myArray);
var_dump($myArray);
}

edbr
02-21-2012, 02:42 AM
im not really on top of this but array(1) { [0]=> string(9) "November " looks a bit like a multiple dimension array so array_unique wont work, there was some disscusion and functions posted on that in the link.
you are adding an array (fetch_assoc) and using push to add it ro another array . I wonder if you can use array_unique before using push??