-
Notifications
You must be signed in to change notification settings - Fork 1
array_get_except
Tonya Mork edited this page Mar 12, 2017
·
3 revisions
The array_get_except()
function walks through the given array, removes the specified elements via the keys you pass into the function, and then returns the new array to you. This function gives the means to remove elements while getting back a new array.
You can remove a specific element at any depth level using dot notation. Or you can remove multiple elements using an array of keys.
Yes. The second parameter can be dot notation to indicate a deeply nested array position.
array array_get_except(
array $subjectArray,
string|array $keys
);
where:
$subjectArray
is the array to work on
$keys
the key or keys that you want to exclude.
Keys can be a single key, a deeply nested key using dot notation, or an array of keys
$user = array(
'user_id' => 504,
'name' => 'Bob Jones',
'social' => array(
'twitter' => '@bobjones',
'website' => 'https://bobjones.com',
),
'languages' => array(
'php' => array(
'procedural' => true,
'oop' => false,
),
'javascript' => true,
'ruby' => false,
),
);
$user = array_get_except( $user, 'social.twitter' );
/**
Returns:
$user = array(
'user_id' => 504,
'name' => 'Bob Jones',
'social' => array(
'website' => 'https://bobjones.com',
),
'languages' => array(
'php' => array(
'procedural' => true,
'oop' => false,
),
'javascript' => true,
'ruby' => false,
),
);*/
$user = array_get_except( $user, array( 'social.twitter', 'languages.php.oop' );
/**
Returns:
$user = array(
'user_id' => 504,
'name' => 'Bob Jones',
'social' => array(
'website' => 'https://bobjones.com',
),
'languages' => array(
'php' => array(
'procedural' => true,
),
'javascript' => true,
'ruby' => false,
),
);*/
$user = array_get_except( $user, array( 'social', 'languages.php' );
/**
Returns:
$user = array(
'user_id' => 504,
'name' => 'Bob Jones',
'languages' => array(
'javascript' => true,
'ruby' => false,
),
);*/