0

I have tried for about three days and I just cannot figure this out. The goal is to have the user input numbers and have it filter out the unique numbers. Say the user inputs the numbers: 24, 24, 56, 23, 1, 3, 24 - the output should be 24, 56, 23, 1, 3. Up to this point, everything appears to be working. The only thing is when I hit the submit button, it doesn't return any values.

    <!DOCTYPE html>
    <!--    e9_1.php
    Project1.php
    -->
    <html lang = "en">
    <head> 
      <title> Project1.php </title>
      <meta charset = "utf-8" />
      <?php

      function unique($strings) {
         $uniqueStrings = array();
         foreach ($strings as $string) {
            foreach ($uniqueStrings as $uString) {
               if ($string == $uString) break;
               if(isset($_POST[‘numbers’])){
                  $str=preg_split("/[\s,]", $_POST['numbers']);
               }
            }
            if ($string != $uString)
               $uniqueStrings[] = $string;
         }
         return $uniqueStrings;
      }
      ?>
    </head>
    <body>
    <?php
    $str = array();
    $uStr=unique($str);
    foreach ($uStr as $st)
    print ("$st <br />");
    ?>

    <br>
    Enter Numbers: <br>
    <form method = "POST">
    <input type="text" name="numbers"/>
    <input type="submit" name="Submit" />

    </body>
    </html>
9
  • 1
    One problem is that you're passing an empty array to your unique() function. Commented Nov 7, 2018 at 0:55
  • And are those curly quotes on $_POST[‘numbers’] in your actual code, or is that just a copy/paste problem? Commented Nov 7, 2018 at 0:56
  • @Don'tPanic, I saw the same like you. So your eyes are OK ! :D Commented Nov 7, 2018 at 1:02
  • 3v4l.org/JDa9o I tested code, it has error in line 6: foreach ($strings as $string) Commented Nov 7, 2018 at 1:02
  • 1
    @NgocNam If you feel you have an appropiate answer (as your snipped could be), you could write it as an answer! Commented Nov 7, 2018 at 1:19

3 Answers 3

1

Here's a working snippet for your task. I've re-written most of it.

<?php

//$_POST['numbers'] = " 24, 24, 56, 23, 1, 3, 24";

function getNumbers($string) {
    $string = preg_replace("/[\s]/","",$string); // remove all possible space, tabs, etc
    $n = explode(",", $string); // a simple explode will be enough then
    return $n;
}

$numbers=array_unique(getNumbers($_POST['numbers'])); // finally use array_unique() to get rid of the doubles.

var_dump($numbers);

// OUTPUT:
array(5) {
  [0]=>
  string(2) "24"
  [2]=>
  string(2) "56"
  [3]=>
  string(2) "23"
  [4]=>
  string(1) "1"
  [5]=>
  string(1) "3"
}

Snippet can be found here: https://3v4l.org/BK0eU

The problems in your code were:

  • empty array as input to function unique
  • preg_replace missing end delimiter
  • $_POST[‘numbers’] with curly quotes
  • coding a function that is already there (array_unique) ...

Note: This simple function does not yet check if there was other input than numbers and commas.

Sign up to request clarification or add additional context in comments.

2 Comments

Sorry if some of my answer is redundant with yours. I just had a slightly different take on it. I think we all agree that reimplementing array_unique is probably not the way to go, though.
all good! I guess it's also good for others to see different implementations of the same task to pick for their specific needs. And you gave me a thumbs up ;) (as I did for your answer)
1

You're getting the user input from a single text input.

<input type="text" name="numbers"/>

That means $_POST['numbers'] is going to be a string, not an array, so your unique function needs to take a string.

If you're looking for unique numbers, it's probably better to do a positive match for sequences of digits rather than splitting on whitespace, or anything else, for that matter.

Your function is quite complex, when PHP already has the array_unique function. It should just be a matter of matching the numbers in the string and running the resulting array through array_unique. Also, I would recommend not referencing a superglobal like $_POST in a helper function like that. If you want it to be reusable, it should take its input as an argument rather than depending on a global value.

Based on all that, I'd write the function like this:

function unique_numbers(string $input): array
{
    preg_match_all('/\d+/', $input, $matches);
    return array_unique($matches[0]);
}

and call it with the posted string

$numbers = unique_numbers($_POST['numbers']);

Comments

0

How about utilize PHP functions to make it simpler?

<!DOCTYPE html>
<html lang = "en">
<head> 
  <title> Project1.php </title>
  <meta charset = "utf-8" />
</head>
<body>
    <?php
    if (isset($_POST['numbers'])) {

        $strings = $_POST['numbers'];
        $str = explode(",", $strings);
        $uStr=array_unique($str);
        echo implode(",", $uStr);
    }
    ?>

    <br>
    Enter Numbers: <br>
    <form method = "POST">
    <input type="text" name="numbers"/>
    <input type="submit" name="Submit" />

</body>
</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.