Advertisement
VladoG

[PHP Basics - Exercises] - 05. Add/Remove Elements

Jul 31st, 2016
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.29 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>Array Indexes</title>
  6. </head>
  7. <body>
  8. <?php
  9.    if (isset($_GET['delimiter']) && isset($_GET['commands'])) {
  10.        $delimiter = $_GET['delimiter'];
  11.        $cmdLine = explode("\n", $_GET['commands']);
  12.        $cmdNum = count($cmdLine);
  13.        var_dump($delimiter);
  14.        var_dump($cmdLine);
  15.        var_dump($cmdNum);
  16.        $array = [];
  17.  
  18.        for ($i = 0; $i < $cmdNum; $i++){
  19.            $command = explode($delimiter,$cmdLine[$i]);
  20.            $cmd = $command[0];
  21.            $val = $command[1];
  22.            if ($cmd == "add"){
  23.                $array[] = $val;
  24.                echo "ADD";
  25.                var_dump($array);
  26.            } else {
  27.                $pos = $val;
  28.                if ($pos >= 0 && $pos < count($array)){
  29.                    echo "REMOVE";
  30.                     array_splice($array,$pos,1);
  31.                     var_dump($array);
  32.                 }
  33.             }
  34.         }
  35.  
  36.         // PRINT Output
  37.         for ($i = 0; $i < count($array); $i++){
  38.            echo $array[$i]."<br>";
  39.         }
  40.     }
  41. ?>
  42.  
  43. <form>
  44.     Delimiter: <input type="text" name="delimiter">
  45.     Input: <textarea name="commands"></textarea>
  46.     <input type="submit">
  47. </form>
  48. </body>
  49. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement