Jump to content

[SOLVED] How do I search a PHP echoed list of numbers?


Recommended Posts

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?

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

 

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.. :'(

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/>';

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++;
}

?>

 

 

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";
}
}
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.