Skip to content

Instantly share code, notes, and snippets.

@endel
Created July 3, 2014 20:14
Show Gist options
  • Save endel/6da6709412e679c32295 to your computer and use it in GitHub Desktop.
Save endel/6da6709412e679c32295 to your computer and use it in GitHub Desktop.
PHP: remove and return key from array
<?php
$name = $attribute['name'];
unset($attribute['name']);
$type = camel_case($attribute['type']);
unset($attribute['type']);
$index = null;
if (isset($attribute['index'])) {
$index = $attribute['index'];
unset($attribute['index']);
}
$default = null;
if (isset($attribute['default'])) {
$default = $attribute['default'];
unset($attribute['default']);
}
<?php
function array_remove(array &$arr, $key) {
if (array_key_exists($key, $arr)) {
$val = $arr[$key];
unset($arr[$key]);
return $val;
}
return null;
}
$name = array_remove($attribute, 'name');
$type = camel_case(array_remove($attribute, 'type'));
$index = array_remove($attribute, 'index');
$default = array_remove($attribute, 'default');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment