0

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>

Edited by blueguy777: forget to add eg code

4
Contributors
8
Replies
72
Views
1 Year
Discussion Span
Last Post by blueguy777
0

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();
}

Edited by minitauros

2

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

0

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);

Edited by blueguy777: some code correction

0

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']);.

0

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 ?

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.