Current location: Hot Scripts Forums » Programming Languages » PHP » duplicate record in array


duplicate record in array

Reply
  #1 (permalink)  
Old 07-13-08, 12:38 PM
zodehala zodehala is offline
Wannabe Coder
 
Join Date: Mar 2007
Posts: 191
Thanks: 0
Thanked 0 Times in 0 Posts
duplicate record in array

i assign approximately 1000 record into an array called $x

how can i list duplicate record in array ?
Reply With Quote
  #2 (permalink)  
Old 07-13-08, 09:59 PM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,515
Thanks: 20
Thanked 109 Times in 106 Posts
Use array_unique http://us3.php.net/manual/en/function.array-unique.php, then compare the length of the array_unique with the original array. If they are different, there are duplicates and you could use array_diff http://us3.php.net/manual/en/function.array-diff.php to find them.
Reply With Quote
  #3 (permalink)  
Old 07-14-08, 09:26 AM
Keith's Avatar
Keith Keith is offline
Community Liaison
 
Join Date: Feb 2004
Posts: 1,232
Thanks: 1
Thanked 11 Times in 11 Posts
You could also use array_count_values(), arsort(), and array_diff():
PHP Code:

// dummy values
$vals = array(
    
'One',
    
'Two',
    
'Three',
    
'Four',
    
'Five',
    
'One',
    
'Two',
    
'Three',
    
'Two',
    
'Three',
);

// get number of instances per value
$vals array_count_values$vals );

// sort values by count from most frequent to least
arsort$vals );

// optional, removes all values that appear only once
$vals array_diff$vals, array( '1' ) );

print_r$vals ); 
...will output:
Code:
Array
(
    [Two] => 3
    [Three] => 3
    [One] => 2
)
__________________
The toxic ZCE
Reply With Quote
  #4 (permalink)  
Old 07-14-08, 01:13 PM
zodehala zodehala is offline
Wannabe Coder
 
Join Date: Mar 2007
Posts: 191
Thanks: 0
Thanked 0 Times in 0 Posts
İ can list users in db using following

PHP Code:

while($row mysql_fetch_array($query)){

    echo 
$row["username"];

i can list duplicate record in array using following
PHP Code:

$array = array('a''b''a''a''c''a''d''a''c''d','e','e','e','e','e','e','e','e','e','e','e','e');

$answer array_count_values($array);
$i=0;
foreach(
$array as $value){
    if(
$answer[$array[$i]] >2){
        echo 
$value.$answer[$array[$i]]."<br/>";
    }
    
$i++;
    

how can i merge both of them ? ( i tried but i can not)
Reply With Quote
  #5 (permalink)  
Old 07-14-08, 01:35 PM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,515
Thanks: 20
Thanked 109 Times in 106 Posts
Maybe this will help:

PHP Code:

$array=array();

while(
$row mysql_fetch_array($query)){ 
    
$array[]=$row["username"]; 

Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ')' Dr. Forensics PHP 3 07-15-06 03:54 PM
Counting duplicate entries in an array. MGCJerry PHP 3 03-24-06 01:22 AM
linking to iframe not working :( j0d JavaScript 5 01-19-04 08:14 PM


All times are GMT -5. The time now is 07:50 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.