bobleny Posted January 28, 2008 Share Posted January 28, 2008 OK, I am echoing out a list of 4,300 numbers. I would like to search this list of numbers for duplicates, then echo out what number is duplicated... In other words, a list of numbers like: 123 456 789 159 357 753 951 987 ... The number 123 should not appear again. If it does show up again, I want to echo the number. How do I do that? Quote Link to comment https://forums.phpfreaks.com/topic/88131-solved-how-do-i-search-a-php-echoed-list-of-numbers/ Share on other sites More sharing options...
toplay Posted January 28, 2008 Share Posted January 28, 2008 Give members as much info as possible when posting so they can help you better. Like: Where is the data being pulled from in the first place (flat file, database/table)? Can you sort it? You say you don't want to echo dups and then you say you do. There's different techniques. With DB you can use "distinct" or "group by". There's sorting the data and writing some PHP code. Put in an array, and removing dups: http://us2.php.net/manual/en/function.array-unique.php Quote Link to comment https://forums.phpfreaks.com/topic/88131-solved-how-do-i-search-a-php-echoed-list-of-numbers/#findComment-450936 Share on other sites More sharing options...
bobleny Posted January 28, 2008 Author Share Posted January 28, 2008 I guess I thought there was enough information there... Currently I have the numbers in an array..... Lets assume that a portion of the numbers look like this: 563 318 394 563 318 983 318 What I need to see is this (In no particular order): 563 563 ------------ 318 318 318 ------------ Does that make more sense? --- I wish they wouldn't stop me from editing my posts after an X amount of time... "The number 123 should not appear again. If it does show up again, I want to echo the number." That doesn't make much sense... Sorry.. :'( Quote Link to comment https://forums.phpfreaks.com/topic/88131-solved-how-do-i-search-a-php-echoed-list-of-numbers/#findComment-450970 Share on other sites More sharing options...
toplay Posted January 28, 2008 Share Posted January 28, 2008 There might be quicker ways using some of the array functions, but here's one approach for now: <?php sort($arrData); // sort your array of numeric values $intCurrentValue = 0; $intDupCount = 0; for ($i = 0, $intCnt = count($arrData); $i < $intCnt; $i++) { if ($intCurrentValue != $arrData[$i]) { if ($intDupCount > 1) { echo str_repeat($intCurrentValue . '<br/>', $intDupCount); // displays number that appeared more than once } $intCurrentValue = $arrData[$i]; $intDupCount = 0; } $intDupCount++; } ?> EDIT: I forgot to put the str_repeat() function in the echo since you want to display the number of times it really is duplicated (which doesn't make much sense to me). If you want to just display the repeating/duplicate number once, then don't use str_repeat() (which makes more sense to me). Something like this makes more sense to display instead: echo $intCurrentValue, ' occurred ', $intDupCount, ' times. <br/>'; Quote Link to comment https://forums.phpfreaks.com/topic/88131-solved-how-do-i-search-a-php-echoed-list-of-numbers/#findComment-450988 Share on other sites More sharing options...
laffin Posted January 28, 2008 Share Posted January 28, 2008 shudn there be an else in there <?php sort($arrData); // sort your array of numeric values $intCurrentValue = 0; $intDupCount = 0; for ($i = 0, $intCnt = count($arrData); $i < $intCnt; $i++) { if ($intCurrentValue != $arrData[$i]) { if ($intDupCount > 1) { echo $intCurrentValue; // displays number that appeared more than once } $intCurrentValue = $arrData[$i]; $intDupCount = 0; } else $intDupCount++; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/88131-solved-how-do-i-search-a-php-echoed-list-of-numbers/#findComment-450996 Share on other sites More sharing options...
toplay Posted January 28, 2008 Share Posted January 28, 2008 shudn there be an else in there No. I want it to increment when logic falls through code inside "if" too. Quote Link to comment https://forums.phpfreaks.com/topic/88131-solved-how-do-i-search-a-php-echoed-list-of-numbers/#findComment-450999 Share on other sites More sharing options...
laffin Posted January 28, 2008 Share Posted January 28, 2008 ok, i see why now. didn hit me that the first pass is zeroed out to get the values. like using a do statement instead of a while statement. Quote Link to comment https://forums.phpfreaks.com/topic/88131-solved-how-do-i-search-a-php-echoed-list-of-numbers/#findComment-451003 Share on other sites More sharing options...
sasa Posted January 28, 2008 Share Posted January 28, 2008 try <?php $a = array(563,318,394,563,318,983,318); $out = array(); foreach ($a as $k) { if (!isset($out[$k])) $out[$k] = 0; $out[$k] += 1; } ksort($out); foreach ($out as $number => $times) { if ($times>1){ for ($i = 0; $i < $times; $i++){ echo $number,"<br />\n"; } echo '--------',"<br />\n\n"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/88131-solved-how-do-i-search-a-php-echoed-list-of-numbers/#findComment-451013 Share on other sites More sharing options...
bobleny Posted February 1, 2008 Author Share Posted February 1, 2008 This will work.... It needs major modifications, but this will work! Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/88131-solved-how-do-i-search-a-php-echoed-list-of-numbers/#findComment-454972 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.