php 从数组中删除所有值,同时保持键完好无损
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2217160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
delete all values from an array while keeping keys intact
提问by jfoucher
Do I really have to do this to reset an array ??
我真的必须这样做才能重置数组吗?
foreach ($array as $i => $value) {
unset($array[$i]);
}
EDIT:
编辑:
this one makes more sense, as the previous one is equivalent to $array=array();
这个更有意义,因为前一个相当于 $array=array();
foreach ($array as $i => $value) {
$array[$i]=NULL;
}
采纳答案by aefxx
Define this function and call it whenever you need it:
定义此函数并在需要时调用它:
function erase_val(&$myarr) {
$myarr = array_map(create_function('$n', 'return null;'), $myarr);
}
// It's call by reference so you don't need to assign your array to a variable.
// Just call the function upon it
erase_val($array);
That's all!
就这样!
回答by Tyler Carter
$keys = array_keys($array);
$values = array_fill(0, count($keys), null);
$new_array = array_combine($keys, $values);
Get the Keys
获取钥匙
Get an array of nulls with the same number of elements
获取具有相同元素数量的空数组
Combine them, using keys and the keys, and the nulls as the values
组合它们,使用键和键,以及空值作为值
As comments suggest, this is easy as of PHP 5.2 with array_fill_keys
正如评论所暗示的那样,从 PHP 5.2 开始,这很容易 array_fill_keys
$new_array = array_fill_keys(array_keys($array), null);
回答by cubaguest
Fill array with old keys and null values
用旧键和空值填充数组
$array = array_fill_keys(array_keys($array), null)
$array = array_fill_keys(array_keys($array), null)
回答by Gordon
There is no build-in function to reset an array to just it's keys.
没有内置函数可以将数组重置为它的键。
An alternative would be via a callback and array_map():
另一种方法是通过回调和array_map():
$array = array( 'a' => 'foo', 'b' => 'bar', 'c' => 'baz' );
With regular callback function
带常规回调函数
function nullify() {}
$array = array_map('nullify', $array);
Or with a lambda with PHP < 5.3
或者使用 PHP < 5.3 的 lambda
$array = array_map(create_function('', ''), $array);
Or with lambda as of PHP 5.3
或者从 PHP 5.3 开始使用 lambda
$array = array_map(function() {}, $array);
In all cases var_dump($array);outputs:
在所有情况下var_dump($array);输出:
array(3) {
["a"]=> NULL
["b"]=> NULL
["c"]=> NULL
}
回答by Darren Murphy
Flip the array to get the keys, then gives all keys the value NULL:
翻转数组以获取键,然后为所有键赋予 NULL 值:
array_fill_keys(array_flip($array), NULL);
About array_fill_keys():
关于 array_fill_keys():
The array_fill_keys() function fills an array with values, specifying keys.
array_fill_keys() 函数用值填充数组,指定键。
About array_flip():
关于 array_flip():
The array_flip() function flips/exchanges all keys with their associated values in an array.
array_flip() 函数翻转/交换所有键及其在数组中的关联值。
回答by amn
foreach($a as &$v)
$v = null;
The reasoning behind setting an array item to null is that an array needs to have a value for each key, otherwise a key makes no sense. That is why it is called a key - it is used to access a value. A null value seems like a reasonable choice here.
将数组项设置为 null 的原因是数组需要为每个键都有一个值,否则键没有意义。这就是它被称为键的原因——它用于访问一个值。空值在这里似乎是一个合理的选择。
Wrap it in a [reusable] procedure:
将其包装在 [可重用] 过程中:
function array_purge_values(&$a)
{
foreach($a as &$v)
$v = null;
}
Keep in mind though that PHP version starting from 5.3 pass values to functions by reference by default, i.e. the ampersand preceding argument variable in the function declaration is redundant. Not only that, but you will get a warning that the notion is deprecated.
请记住,从 5.3 开始的 PHP 版本默认通过引用将值传递给函数,即函数声明中的 & 前面的参数变量是多余的。不仅如此,您还会收到警告,指出该概念已被弃用。
回答by qwerty_igor
If you need to nullify the values of a associative array you can walk the whole array and make a callback to set values to null thus still having keys
如果您需要取消关联数组的值,您可以遍历整个数组并进行回调以将值设置为 null 从而仍然有键
array_walk($ar,function(&$item){$item = null;});
In case if you need to nullify the whole array just reassign it to empty one
如果您需要取消整个数组,只需将其重新分配为空数组
$ar = array();
回答by NYCBilly
This is a fairly old topic, but since I referenced to it before coming up with my own solution for a more specific result, so therefore I will share with you that solution.
这是一个相当古老的话题,但由于我在提出自己的解决方案以获得更具体的结果之前参考了它,因此我将与您分享该解决方案。
The desired result was to nullify all values, while keeping keys, and for it to recursively search the array for sub-arrays as well.
期望的结果是在保留键的同时使所有值无效,并且它也递归搜索数组以查找子数组。
RECURSIVELY SET MULTI-LEVEL ARRAY VALUES TO NULL:
递归地将多级数组值设置为 NULL:
function nullifyArray(&$arrayData) {
if (is_array($arrayData)) {
foreach ($arrayData as $aKey => &$aValue) {
if (is_array($aValue)) {
nullifyArray($aValue);
} else {
$aValue = null;
}
}
return true; // $arrayData IS an array, and has been processed.
} else {
return false; // $arrayData is NOT an array, no action(s) were performed.
}
}
And here is it in use, along with BEFOREand AFTERoutput of the array contents.
这里正在使用它,以及数组内容的BEFORE和AFTER输出。
PHP code to create a multilevel-array, and call the nullifyArray() function:
创建多级数组的 PHP 代码,并调用 nullifyArray() 函数:
// Create a multi-level array.
$testArray = array(
'rootKey1' => 'rootValue1',
'rootKey2' => 'rootValue2',
'rootArray1' => array(
'subKey1' => 'subValue1',
'subArray1' => array(
'subSubKey1' => 'subSubValue1',
'subSubKey2' => 'subSubValue2'
)
)
);
// Nullify the values.
nullifyArray($testArray);
BEFORE CALL TO nullifyArray():
在调用 nullifyArray() 之前:
Array
(
[rootKey1] => rootValue1
[rootKey2] => rootValue2
[rootArray1] => Array
(
[subKey1] => subValue1
[subArray1] => Array
(
[subSubKey1] => subSubValue1
[subSubKey2] => subSubValue2
)
)
)
AFTER CALL TO nullifyArray():
调用 nullifyArray() 后:
Array
(
[rootKey1] =>
[rootKey2] =>
[rootArray1] => Array
(
[subKey1] =>
[subArray1] => Array
(
[subSubKey1] =>
[subSubKey2] =>
)
)
)
I hope it helps someone/anyone, and Thank You to all who previously answered the question.
我希望它可以帮助某人/任何人,并感谢所有之前回答过这个问题的人。
回答by Sairam
回答by dev-null-dweller
Why not making an array with required keys and asinging it to variable when you want reset it?
为什么不使用所需的键创建一个数组并在您想要重置它时将其设置为变量?
function resetMyArr(&$arr)
{
$arr = array('key1'=>null,'key2'=>null);
}

