Here is my solution:
<?php
$arr = explode(" ", readline());
$input = readline();
while ($input != "Stop") {
$args = explode(" ", $input);
$command = $args[0];
switch ($command) {
case 'Delete':
if (($args[1] + 1) < count($arr) && ($args[1] + 1) >= 0) {
array_splice($arr, $args[1] + 1, 1);//removes 1 next element in the array with nothing
}
break;
case 'Swap':
if (in_array($args[1], $arr) && in_array($args[2], $arr)) {
$index1 = array_search($args[1], $arr);
$index2 = array_search($args[2], $arr);
array_splice($arr, $index1, 1, $args[2]);//replace a word with another
array_splice($arr, $index2, 1, $args[1]);//replace another word with a word
}
break;
case 'Put':
if (($args[2] - 1) <= count($arr) && ($args[2] - 1) >= 0) {
array_splice($arr, $args[2] - 1, 0, $args[1]);//add the new element
}
break;
case 'Sort':
rsort($arr);//sorting in descending order
break;
case 'Replace':
if (in_array($args[2], $arr)) {
array_splice($arr, array_search($args[2], $arr), 1, $args[1]);//replace the 2nd element with the 1st
}
break;
}
$input = readline();
}
echo implode(" ", $arr);