A better way would be to use array_splice
Then just create a function to wrap it [array_pluck], which plucks out the array item, and updates the original input.
<?php
$input = ["123","456","789"];
function array_pluck(&$array, $key) {
if (isset($array[$key])) {
return array_splice($array, $key, 1)[0];
}
}
echo array_pluck($input, 1); //456
print_r($input);
https://3v4l.org/J42A5
If you're looping over the array, and then you want to remove the items, you could also use a generator.
<?php
$input = ["123","456","789"];
function array_pluck_gen(&$array) {
foreach ($array as $k => $v) {
unset($array[$k]);
yield $v;
}
}
foreach (array_pluck_gen($input) as $value) {
echo $value;
}
print_r($input);
Or just unset it.
<?php
$input = ["123","456","789"];
foreach ($input as $key => $value) {
unset($input[$key]);
echo $value;
}
print_r($input);