0

I want something that works like array_pop() or array_shift(), so it returns the removed element, but I'd like to specify by key which element to remove.

Example:

$arr = ['a' => 1, 'b' => 2, 'c' => 3]; 
$removed = array_remove($arr, 'b');

print_r($arr); // Outputs: Array(['a'] => 1, ['c'] => 3 )
print_r($removed); // Outputs: 2

Now I'am using

$arr = ['a' => 1, 'b' => 2, 'c' => 3]; 
$removed = $arr['b'];
unset($arr['b']);
ACs
  • 1,325
  • 2
  • 21
  • 39

1 Answers1

1

array_splice() http://php.net/manual/en/function.array-splice.php Omit replacement.

MIvanIsten
  • 481
  • 3
  • 7
  • That won't work with hash maps and the removed value is not returned. The second argument must be an integer and the return type is an array. – Pedro Amaral Couto Mar 22 '18 at 14:33