Use array_diff_key to remove
$remove = ['telephone', 'country'];
$remaining = array_diff_key($post, array_flip($remove));
You could use array_intersect_key if you wanted to supply an array of keys to keep.
Use array_diff_key to remove
$remove = ['telephone', 'country'];
$remaining = array_diff_key($post, array_flip($remove));
You could use array_intersect_key if you wanted to supply an array of keys to keep.
It looks like the function extract() would be a better tool for what you're trying to do (assuming it's extract all key/values from an array and assign them to variables with the same names as the keys in the local scope). After you've extracted the contents, you could then unset the entire $post, assuming it didn't contain anything else you wanted.
However, to actually answer your question, you could create an array of the keys you want to remove and loop through, explicitly unsetting them...
$removeKeys = array('name', 'email');
foreach($removeKeys as $key) {
unset(
key]);
}
...or you could point the variable to a new array that has the keys removed...
$arr = array_diff_key($arr, array_flip($removeKeys));
...or pass all of the array members to unset()...
unset($arr['name'], $arr['email']);
Videos
This is an easy one-liner:
$result = array_diff(Schema::getColumnListing('places'), ['Id', 'State', 'Terrain']);
Check this code,
$arr = ["0" => "Id", "1" => "Name"
];
$columndata = [];
foreach (
key => &$value) {
if(in_array($value,["Id",'State','Terrain']){
unset($value);
}else{
$columndata[] = $value;
}
}
$columndata = array_values(array_filter($columndata));
print_r($columndata);
Error was coming because, after check id, you are unsetting that value. And then trying to save that value in columndata.
Here is working code
There are different ways to delete an array element, where some are more useful for some specific tasks than others.
Deleting a Single Array Element
If you want to delete just one single array element you can use unset() and alternatively array_splice().
By key or by value?
If you know the value and don't know the key to delete the element you can use array_search() to get the key.
This only works if the element doesn't occur more than once, since array_search() returns the first hit only.
unset() Expression
Note: When you use unset() the array keys wonโt change.
If you want to reindex the keys you can use array_values() after unset(),
which will convert all keys to numerically enumerated keys starting from 0
(the array remains a list).
Example Code:
$array = [0 => "a", 1 => "b", 2 => "c"];
unset($array[1]);
// โ Key of element to delete
Example Output:
[
[0] => a
[2] => c
]
array_splice() Function
If you use array_splice() the (integer) keys will automatically be reindex-ed,
but the associative (string) keys won't change โ as opposed to array_values() after unset(),
which will convert all keys to numerical keys.
Note: array_splice()
needs the offset, not the key, as the second parameter; offset = array_flip(array_keys(array))[key].
Example Code:
$array = [0 => "a", 1 => "b", 2 => "c"];
array_splice($array, 1, 1);
// โ Offset of element to delete
Example Output:
[
[0] => a
[1] => c
]
array_splice(), same as unset(), take the array by reference. You donโt assign the return values back to the array.
Deleting Multiple Array Elements
If you want to delete multiple array elements and donโt want
to call unset() or array_splice() multiple times you can use the functions array_diff() or
array_diff_key() depending on whether you know the values or the keys of the elements to remove from the array.
array_diff() Function
If you know the values of the array elements which you want to delete, then you can use array_diff().
As before with unset() it wonโt change the keys of the array.
Example Code:
$array = [0 => "a", 1 => "b", 2 => "c", 3 => "c"];
$array = array_diff($array, ["a", "c"]);
// โโโโโโโโโโ
// Array values to delete
Example Output:
[
[1] => b
]
array_diff_key() Function
If you know the keys of the elements which you want to delete, then you want to use array_diff_key().
You have to make sure you pass the keys as keys in the second parameter and not as values.
Keys wonโt reindex.
Example Code:
$array = [0 => "a", 1 => "b", 2 => "c"];
$array = array_diff_key($array, [0 => "xy", "2" => "xy"]);
// โ โ
// Array keys of elements to delete
Example Output:
[
[1] => b
]
If you want to use unset() or array_splice() to delete multiple elements with the same value you can use
array_keys() to get all the keys for a specific value
and then delete all elements.
array_filter() Function
If you want to delete all elements with a specific value in the array you can use array_filter().
Example Code:
$array = [0 => "a", 1 => "b", 2 => "c"];
$array = array_filter($array, static function ($element) {
return $element !== "b";
// โ
// Array value which you want to delete
});
Example Output:
[
[0] => a
[2] => c
]
It should be noted that unset() will keep indexes untouched, which is what you'd expect when using string indexes (array as hashtable), but can be quite surprising when dealing with integer indexed arrays:
$array = array(0, 1, 2, 3);
unset($array[2]);
var_dump($array);
/* array(3) {
[0]=>
int(0)
[1]=>
int(1)
[3]=>
int(3)
} */
$array = array(0, 1, 2, 3);
array_splice($array, 2, 1);
var_dump($array);
/* array(3) {
[0]=>
int(0)
[1]=>
int(1)
[2]=>
int(3)
} */
So array_splice() can be used if you'd like to normalize your integer keys. Another option is using array_values() after unset():
$array = array(0, 1, 2, 3);
unset($array[2]);
$array = array_values($array);
var_dump($array);
/* array(3) {
[0]=>
int(0)
[1]=>
int(1)
[2]=>
int(3)
} */
The following doesn't fullfill your requirement in complete since it's not in-situ. But maybe you're ok with copying the array:
$v = array("lol"=>"blub", "lal"=>"blab", "lulz"=>"gagh");
$k = array("lol", "lulz");
var_dump(array_diff_key($v, array_flip($k)));
[ run it on codepad ]
You could create an array of the keys you want to remove and loop through, explicitly unsetting them. Examples:
$removeKeys = array('name', 'email');
foreach($removeKeys as $key) {
unset($badElements[$key]);
}
Or you could point the variable to a new array that has the keys removed.
$badElements = array_diff_key($badElements, array_flip($removeKeys));
or pass all of the array members to unset().
unset($badElements['name'], $badElements['email'])
You can pass the $user as reference like this :
// check this --------------v
foreach ( $data['users'] as &$user) {
unset($user['password']);
}
Try:
foreach ( $data['users'] as $key => $user) {
unset($data['users'][$key]['password']);
}
Or
foreach ( $data['users'] as &$user) {
unset($user['password']);
}