PHP 删除空的空数组键/值,同时保留键/值,否则不为空/空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9568044/
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
PHP remove empty, null Array key/values while keeping key/values otherwise not empty/null
提问by chris
I have an array thats got about 12 potential key/value pairs. That are based off a _POST/_GET
我有一个数组,它有大约 12 个潜在的键/值对。这是基于 _POST/_GET
The keys are not numeric as in 0-n, and I need to retain the keys with there values where applicable. My issue is I know that on occasion a key will be passed where the value is null, empty, or equal to ''. In the event thats the case I want to trim out those keys before processing my array. As running down the line without something there is going to break my script.
键不是 0-n 中的数字,我需要在适用的情况下保留带有值的键。我的问题是我知道有时会传递值为空、空或等于 '' 的键。在这种情况下,我想在处理我的数组之前修剪掉这些键。由于没有任何东西就下线,这会破坏我的脚本。
Now a while back I either made or found this function (I don't remember which its been in my arsenal for a while, either way though).
不久前,我创建或发现了这个函数(我不记得它在我的武器库中有一段时间了,不管怎样)。
function remove_array_empty_values($array, $remove_null_number = true)
{
$new_array = array();
$null_exceptions = array();
foreach($array as $key => $value)
{
$value = trim($value);
if($remove_null_number)
{
$null_exceptions[] = '0';
}
if(!in_array($value, $null_exceptions) && $value != "")
{
$new_array[] = $value;
}
}
return $new_array;
}
What I would love to do is very similar to this, however this works well with arrays that can have n-n key values and I am not dependent upon the key as well as the value to determine whats what where and when. As the above will just remove everything basically then just rebuilt the array. Where I am stuck is trying to figure out how to mimic the above function but where I retain the keys I need.
我想做的与此非常相似,但是这适用于可以具有 nn 个键值的数组,并且我不依赖于键和值来确定何时何地是什么。由于上述内容将基本上删除所有内容,然后重建数组。我被卡住的地方是试图弄清楚如何模仿上述功能,但我在哪里保留了我需要的钥匙。
回答by Aleks G
If I understand correctly what you're after, you can use array_filter()
or you can do something like this:
如果我正确理解您的要求,您可以使用array_filter()
或执行以下操作:
foreach($myarray as $key=>$value)
{
if(is_null($value) || $value == '')
unset($myarray[$key]);
}
回答by Tomas Gonzalez
If you want a quick way to remove NULL, FALSE and Empty Strings (""), but leave values of 0 (zero), you can use the standard php function strlen as the callback function:
如果您想快速删除 NULL、FALSE 和空字符串 (""),但保留值 0(零),您可以使用标准的 php 函数 strlen 作为回调函数:
// removes all NULL, FALSE and Empty Strings but leaves 0 (zero) values
$result = array_filter( $array, 'strlen' );
Source: http://php.net/manual/en/function.array-filter.php#111091
来源:http: //php.net/manual/en/function.array-filter.php#111091
回答by Jon
array_filter
is a built-in function that does exactly what you need. At the most you will need to provide your own callback that decides which values stay and which get removed. The keys will be preserved automatically, as the function description states.
array_filter
是一个内置函数,可以完全满足您的需求。您最多需要提供自己的回调来决定保留哪些值以及删除哪些值。键将自动保存,如功能描述所述。
For example:
例如:
// This callback retains values equal to integer 0 or the string "0".
// If you also wanted to remove those, you would not even need a callback
// because that is the default behavior.
function filter_callback($val) {
$val = trim($val);
return $val != '';
}
$filtered = array_filter($original, 'filter_callback');
回答by Anwarul Islam
use +1 with your key variable to skip null key in array
将 +1 与键变量一起使用以跳过数组中的空键
foreach($myarray as $key=>$value)
{
echo $key+1; //skip null key
}