1

Is there a native PHP function for removing an element from an associative array, and returning the value?

Like unset() but with a return value, or array_shift() where you can specify the index to shift?

$element = unset($array['index']);

$element = array_shift($array, 'index');

I know it's easy to do, I'm just curious if there's an elegant one-liner for doing this.

BadHorsie
  • 14,135
  • 30
  • 117
  • 191

1 Answers1

2

Looking quickly at the official PHP documentation, in the current version (7.2) doesn't have a function that removes and returns an element by the key.

But as you mentioned there are several ways to solve this problem. As you can see at: https://stackoverflow.com/a/10898827/4214312

Abraão Levi
  • 101
  • 3