- var_export: documentation ( source)
- array_keys: documentation ( source)
- max: documentation ( source)
<?php
class SomeHelperClass {
public $_arr = [];
public function addValue($value, $key = null): void
{
if ($key !== null) {
$this->_arr[$key] = $value;
} elseif (!$this->_arr) {
$this->_arr[] = $value;
} else {
$this->_arr[max(array_keys($this->_arr)) + 1] = $value;
}
}
}
$test = new SomeHelperClass;
var_export($test->_arr);
echo "\n";
$test->addValue(1);
var_export($test->_arr);
echo "\n";
$test->addValue(2);
var_export($test->_arr);
echo "\n";
$test->addValue(3, 4);
var_export($test->_arr);
echo "\n";
$test->addValue(4, 3);
var_export($test->_arr);
echo "\n";
$test->addValue(5);
var_export($test->_arr);