faqts : Computers : Programming : Languages : PHP : Common Problems : Arrays

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

7 of 9 people (78%) answered Yes
Recently 6 of 8 people (75%) answered Yes

Entry

Is there any way to use array_unique function on result from mysql_fetch_array, but keep named keys (col. names) instead of numbered keys?

Jul 9th, 2001 07:57
Ivan L., Matthews, http://www.php.net/manual/en/function.mysql-fetch-assoc.php


I do not really understand why you would want to array_unique a row (if 
two fields have the same *value*, that would erase the second field). 
If you want an array without numerical indices use mysql_fetch_assoc() 
(php>4.0.3) (there is a drawback to using this function however : if 
two of your fields have the same *name* (think of a JOIN), you only get 
one of them.) 
If you do not have php>4.0.3 you could get rid of the entries with 
numeric keys with
for(reset($row);list($key,$value)=each($row);){
  if(is_numeric($key))unset($row[$key]);
}  
array_unique does not care about numeric or string indices, it just 
removes duplicates in the order it finds them. So if what you want is 
really what you say array_unique(array_reverse($row)) would do the job.