How to remove duplicate mobile numbers from input text box1 and print the result into text box2.

Example mobile nos:
7795012345
7795012345
7795012345
7411312354
7411312354
9900454321
9900454321
9632554123
9632554123
output should be:
7795012345
7411312354
9900454321
9632554123

<?php
$myarray = array();
$myarray['numbers']=$_POST['numbers'];

foreach($myarray as $num){
       $numbers=$num;
}
$unique = array_unique($numbers); 
$output=implode("<br />",$unique);
?>

<form method="post">

Result  <textarea rows="5" cols="30"><?php echo $output;?></textarea><hr/>

<textarea name="numbers" class="span8 note" id="numbers" rows="5" cols="30"></textarea><hr/>

<input type="submit" value="ADD"/>

</form>

Recommended Answers

All 8 Replies

Your code with comment:

$myarray = array();
$myarray['numbers']=$_POST['numbers'];
foreach($myarray as $num){
       $numbers=$num; // You aren't creating an array here. You are giving $numbers the value of the CURRENT number in the loop. PLUS your numbers are stored in $myarray['numbers'], not in $myarray ;).
}
$unique = array_unique($numbers); 
$output=implode("<br />",$unique);

What it should be:

// Assign $_POST['numbers'] to $numbers if it exists, otherwise make it an empty array.
$numbers = !empty($_POST['numbers']) ? $_POST['numbers'] : array();

// Output the result.
$output = implode('<br>', array_unique($numbers));

Nice find on the array_unique by the way :). If you need more explanation, let me know! Some explanation on the $var = $condition ? true : false line I used:

// This line..
$numbers = !empty($_POST['numbers']) ? $_POST['numbers'] : array();

// ..is shortcode for:
if (!empty($_POST['numbers'])) {
    $numbers = $_POST['numbers'];
}
else {
    $numbers = array();
}

the given solution is not printing values

I only included the PHP to remove the duplicate numbers. You will have to print them yourself ;).

Current Code:

<?php
$numbers = array();
$numbers[] = $_POST['numbers'];

$output=implode(',', $numbers);
echo $output;
?>

Output Result:
7795012345 7795012345 7795012345 7411312354 7411312354 9900454321 9900454321 9632554123 9632554123
how to put comma after each 10 digit mobile number?
Example:

$numbers = array(7795012345,7795012345,7795012345,7411312354,7411312354,9900454321,9900454321,9632554123,9632554123);

Mate, do you have any idea what you are doing at all? :P What is is that you are trying to do? Are you trying to get an array of unique phone numbers or are you trying to output a comma separated list? Because if it is the latter, the only thing you'd need is echo implode(',', $_POST['numbers']);.

bluegue777 why don't you give us your whole project to do it for you? The first answer was solved by minitauros , did you thanked him and marked this as solved ? , No you moved on to the next question that you had in mind. Instead of thanking you had remarks as “the given solution is not printing values” did you asked for it ? And now a whole new different demand , just a quick question , what do you think that we are the people here trying to help ?

Sorry & thanks to all

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.