logo-dw

Go Back   Dreamweaver Club Forums > Hand Coders Forum > PHP
Register FAQ Members List Search Today's Posts Mark Forums Read

Reply
 
Thread Tools Display Modes
Old 02-19-2012, 06:10 PM   #1
gavimobile
gavimobile's Avatar
 
Join Date: Mar 2007
Posts: 187
Default adding to an array on loop than remove all duplicates

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.
PHP Code:
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
__________________
there are many technicians today, but theres only 1 motek! motekpc.com
gavimobile is offline   Reply With Quote
Old 02-19-2012, 06:14 PM   #2
gavimobile
gavimobile's Avatar
 
Join Date: Mar 2007
Posts: 187
Default

my guess is that
$myArray = array();
needs to be before the loop, but im still missing something else.
__________________
there are many technicians today, but theres only 1 motek! motekpc.com
gavimobile is offline   Reply With Quote
Old 02-20-2012, 05:17 AM   #3
edbr
edbr's Avatar
 
Join Date: Aug 2005
Location: Bali
Posts: 11,176
Default

try this


$myArray = array();
array_push($myArray, $month);
__________________
If you're happy and you know it shake your meds!
different style links examples

Flight / Hotel search
Free script download
Bali Villas
edbr is offline   Reply With Quote
Old 02-20-2012, 07:43 AM   #4
gavimobile
gavimobile's Avatar
 
Join Date: Mar 2007
Posts: 187
Default

still doesn't seem to be working.
i see the output but there is still doubles

i tried
PHP Code:
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

PHP Code:
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

PHP Code:
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);

__________________
there are many technicians today, but theres only 1 motek! motekpc.com
gavimobile is offline   Reply With Quote
Old 02-20-2012, 08:21 AM   #5
edbr
edbr's Avatar
 
Join Date: Aug 2005
Location: Bali
Posts: 11,176
Default

second one looks correct but just read this, if it helps

some functions there
http://php.net/manual/en/function.array-unique.php
__________________
If you're happy and you know it shake your meds!
different style links examples

Flight / Hotel search
Free script download
Bali Villas
edbr is offline   Reply With Quote
Old 02-20-2012, 08:45 AM   #6
gavimobile
gavimobile's Avatar
 
Join Date: Mar 2007
Posts: 187
Default

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

PHP Code:
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); 

__________________
there are many technicians today, but theres only 1 motek! motekpc.com
gavimobile is offline   Reply With Quote
Old 02-21-2012, 02:42 AM   #7
edbr
edbr's Avatar
 
Join Date: Aug 2005
Location: Bali
Posts: 11,176
Default

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??
__________________
If you're happy and you know it shake your meds!
different style links examples

Flight / Hotel search
Free script download
Bali Villas
edbr is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 09:17 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Copyright 2006 DreamweaverClub.com