Advertisement
Mihail_Atnsv

arrayManipulationBasics

Oct 27th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.41 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: Toshiba
  5.  * Date: 27.10.2018 г.
  6.  * Time: 10:14
  7.  */
  8.  
  9. $arr = array_map('intval', explode(" ", readline()));
  10. $line = readline();
  11.  
  12. while ($line != "end") {
  13.  
  14.     $tokens = explode(" ", $line);
  15.     $command = $tokens[0];
  16.  
  17.     switch ($command) {
  18.         case "Add":
  19.             $numToAdd = intval($tokens[1]);
  20.             $arr[] = add($arr, $numToAdd);
  21.             break;
  22.         case "Remove":
  23.             $numToRemove = intval($tokens[1]);
  24.             $arr = remove($arr, $numToRemove);
  25.             break;
  26.         case "RemoveAt":
  27.             $indexToRemove = intval($tokens[1]);
  28.             $arr = removeAt($arr, $indexToRemove);
  29.             break;
  30.         case "Insert":
  31.             $numToInsert = intval($tokens[1]);
  32.             $indexToInsert = intval($tokens[2]);
  33.             $arr = insert($arr, $numToInsert, $indexToInsert);
  34.             break;
  35.  
  36.     }
  37.  
  38.     $line = readline();
  39. }
  40.  
  41. echo implode(" ", $arr);
  42.  
  43. function add($arr, $numToAdd)
  44. {
  45.     return $arr[] = $numToAdd;
  46. }
  47.  
  48. function remove($arr, $numToRemove)
  49. {
  50.     array_search($numToRemove, $arr);
  51.     unset($arr[$numToRemove]);
  52.     return $arr;
  53. }
  54.  
  55. function removeAt($arr, $indexToRemove)
  56. {
  57.     array_splice($arr, $indexToRemove, 1);
  58.     return $arr;
  59. }
  60.  
  61. function insert($arr, $numToInsert, $indexToInsert)
  62. {
  63.     array_splice($arr, $indexToInsert, 0, $numToInsert);
  64.     return $arr;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement