Monstre
Monstre

Reputation: 11

PHP - Array from User Input

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>

Upvotes: 0

Views: 2854

Answers (3)

ACD
ACD

Reputation: 1431

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>

Upvotes: 0

Don&#39;t Panic
Don&#39;t Panic

Reputation: 41810

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

Upvotes: 1

Jeff
Jeff

Reputation: 6953

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.

Upvotes: 1

Related Questions