I have an array with 4 values. I would like to remove the value at the 2nd position and then have the rest of the key's shift down one.
$b = array(123,456,789,123);
Before Removing the Key at the 2nd position:
Array ( [0] => 123 [1] => 456 [2] => 789 [3] => 123 )
After I would like the remaining keys to shift down one to fill in the space of the missing key
Array ( [0] => 123 [1] => 789 [2] => 123 )
I tried using unset() on the specific key, but it would not shift down the remaining keys. How do I remove a specific key in an array using php?
You need array_values($b)
in order to re-key the array so the keys are sequential and numeric (starting at 0).
The following should do the trick:
$b = array(123,456,789,123);
unset($b[1]);
$b = array_values($b);
echo "<pre>"; print_r($b);
It is represented that your input data is an indexed array (there are no gaps in the sequence of the integer keys which start from zero). I'll compare the obvious techniques that directly deliver the desired result from the OP's sample data.
unset()
then array_values()
unset($b[1]);
$b = array_value($b);
unset()
can receive multiple parameters, so if more elements need to be removed, then the number of function calls remains the same. e.g. unset($b[1], $b[3], $b[5]);
unset()
cannot be nested inside of array_values()
to form a one-liner because unset()
modifies the variable and returns no value.unset()
is not particularly handy for removing elements using a dynamic whitelist/blacklist of keys.array_splice()
array_splice($b, 1, 1);
// (input array, starting position, number of elements to remove)
array_splice()
can remove a single element or, at best, remove multiple consecutive elements. If you need to remove non-consecutive elements you would need to make additional function calls.array_splice()
does not require an array_values()
call because "Numerical keys in input are not preserved" -- this may or may not be desirable in certain situations.array_filter()
nested in array_values()
array_values(
array_filter(
$b,
function($k) {
return $k != 1;
},
ARRAY_FILTER_USE_KEY
)
)
in_array()
call with a whitelist/blacklist of keys in the custom function.use()
.array_diff_key()
nested in array_values()
array_values(
array_diff_key(
$b,
[1 => '']
)
);
array_diff_key()
really shines when there is a whitelist/blacklist array of keys (which may have a varying element count). PHP is very swift at processing keys, so this function is very efficient at the task that it was designed to do.array_diff_key()
are completely irrelevant -- they can be null
or 999
or 'eleventeen'
-- only the keys are respected.array_diff_key()
does not have any scoping challenges, compared to array_filter()
, because there is no custom function called.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With