- var_dump: documentation ( source)
- array_splice: documentation ( source)
<?php
class Based {
private array $arr = ["zero", "one", "two", "three", "four", "five"];
public function removeElementFromArr (int $index): void {
array_splice($this->arr, $index, 1);
if ($index === 0) {
throw new Exception("Cannot remove the first element of an array, reason: https://www.youtube.com/watch?v=bvlSQz7cBr0");
}
}
public function getArr (): array {
return $this->arr;
}
public function setArr (array $newArr) {
$this->arr = $newArr;
}
}
$obj = new Based();
var_dump($obj->getArr());
$obj->removeElementFromArr(3);
var_dump($obj->getArr());
$obj->removeElementFromArr(0);