Advertisement
braveheart1989

05. Add/Remove Elements

Jul 4th, 2016
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.68 KB | None | 0 0
  1. <?php
  2. if(isset($_GET['delimiter']) && isset($_GET['commands'])) {
  3.     $delimiter = $_GET['delimiter'];
  4.     $commands = $_GET['commands'];
  5.     $commands = explode("\n",$commands);
  6.     $commands = array_map('trim',$commands);
  7.     $result = [];
  8.     for ($i = 0; $i < count($commands); $i++) {
  9.         $command = explode($delimiter,$commands[$i]);
  10.         $commandKey = $command[0];
  11.         $commandValue = $command[1];
  12.  
  13.         if($commandKey=="add") {
  14.             array_push($result,$commandValue);
  15.         }
  16.         else {
  17.             array_slice($result,$commandValue,1);
  18.         }
  19.     }
  20.     for ($i = 0; $i < count($result); $i++) {
  21.         echo $result[$i] . "<br>";
  22.     }
  23.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement