How can I delete an element from a multi-dimensional array given a key?
如何在给定键的情况下从多维数组中删除元素?
I am hoping for this to be greedy so that it deletes all elements in an array that match the keys I pass in. I have this so far where I can traverse a multi-dimensional array but I can't unset the key I need to because I don't have a reference to it!
我希望这是贪婪的,这样它就会删除数组中与我传入的键相匹配的所有元素。到目前为止,我已经可以遍历多维数组,但是我无法取消我需要的键。因为我没有参考它!
function traverseArray($array, $keys)
{
foreach($array as $key=>$value)
{
if(is_array($value))
{
traverseArray($value);
} else {
if(in_array($key, $keys))
{
//unset(what goes here?)
}
}
}
}
4 个解决方案
#1
8
The following code works (and doesn't use deprecated stuff), just tested it:
以下代码有效(并且不使用已弃用的东西),只是测试了它:
function traverseArray(&$array, $keys) {
foreach ($array as $key => &$value) {
if (is_array($value)) {
traverseArray($value, $keys);
} else {
if (in_array($key, $keys)){
unset($array[$key]);
}
}
}
}
#2
1
You could use pass by reference, declare your function like this:
你可以使用pass by reference,声明你的函数如下:
function traverseArray(&$array, $keys)
{
foreach($array as $key=>$value)
{
if(is_array($value))
{
traverseArray($value, $keys);
}else{
if(in_array($key, $keys)){
unset($array[$key]);
}
}
}
}
then you can unset the key and it will vanish from the original passed value too since the $array
in the function is merely a pointer to the array you passed so it updates that array.
那么你可以取消设置密钥,它也将从原始传递的值中消失,因为函数中的$数组只是指向你传递的数组的指针,因此它更新了该数组。
unset($array[$key]);
For more information check the php documentation on passing by reference
有关更多信息,请查看有关通过引用传递的php文档
#3
0
You can do this
你可以这样做
unset($array[$key]);
because $array
will not be a copy of the original array, just a reference to it, so any modifications will hold.
因为$ array不是原始数组的副本,只是对它的引用,所以任何修改都会成立。
Also, you have a small bug in your snippet: when you make the recursive call, you forget the pass the $keys
parameter.
此外,您的代码段中有一个小错误:当您进行递归调用时,您忘记了传递$ keys参数。
#4
0
and don't forget to modify foreach:
并且不要忘记修改foreach:
foreach($array as $key=>&$value)
#1
8
The following code works (and doesn't use deprecated stuff), just tested it:
以下代码有效(并且不使用已弃用的东西),只是测试了它:
function traverseArray(&$array, $keys) {
foreach ($array as $key => &$value) {
if (is_array($value)) {
traverseArray($value, $keys);
} else {
if (in_array($key, $keys)){
unset($array[$key]);
}
}
}
}
#2
1
You could use pass by reference, declare your function like this:
你可以使用pass by reference,声明你的函数如下:
function traverseArray(&$array, $keys)
{
foreach($array as $key=>$value)
{
if(is_array($value))
{
traverseArray($value, $keys);
}else{
if(in_array($key, $keys)){
unset($array[$key]);
}
}
}
}
then you can unset the key and it will vanish from the original passed value too since the $array
in the function is merely a pointer to the array you passed so it updates that array.
那么你可以取消设置密钥,它也将从原始传递的值中消失,因为函数中的$数组只是指向你传递的数组的指针,因此它更新了该数组。
unset($array[$key]);
For more information check the php documentation on passing by reference
有关更多信息,请查看有关通过引用传递的php文档
#3
0
You can do this
你可以这样做
unset($array[$key]);
because $array
will not be a copy of the original array, just a reference to it, so any modifications will hold.
因为$ array不是原始数组的副本,只是对它的引用,所以任何修改都会成立。
Also, you have a small bug in your snippet: when you make the recursive call, you forget the pass the $keys
parameter.
此外,您的代码段中有一个小错误:当您进行递归调用时,您忘记了传递$ keys参数。
#4
0
and don't forget to modify foreach:
并且不要忘记修改foreach:
foreach($array as $key=>&$value)