php 在 array_filter() 之后,如何重置键以从 0 开始按数字顺序排列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3401850/
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
After array_filter(), how can I reset the keys to go in numerical order starting at 0
提问by jel402
I just used array_filter to remove entries that had only the value '' from an array, and now I want to apply certain transformations on it depending on the placeholder starting from 0, but unfortunately it still retains the original index. I looked for a while and couldn't see anything, perhaps I just missed the obvious, but my question is...
我只是使用 array_filter 从数组中删除只有值 '' 的条目,现在我想根据从 0 开始的占位符对其应用某些转换,但不幸的是它仍然保留了原始索引。我看了一会儿,什么也没看到,也许我只是错过了明显的,但我的问题是......
How can I easily reset the indexes of the array to begin at 0 and go in order in the NEW array, rather than have it retain old indexes?
如何轻松地将数组的索引重置为从 0 开始并在新数组中按顺序排列,而不是保留旧索引?
回答by Daniel Vandersluis
If you call array_values
on your array, it will be reindexed from zero.
如果您调用array_values
数组,它将从零重新索引。
回答by user2182143
If you are using Array filter do it as follows
如果您使用的是数组过滤器,请按如下方式操作
$NewArray = array_values(array_filter($OldArray));
回答by mickmackusa
I worry about how many programmers have innocently copy/pasted the array_values(array_filter())
method into their codes-- I wonder how many programmers unwittingly ran into problems because of array_filter's greed. Or worse, how many people never discovered that the function purges too many values from the array...
我担心有多少程序员无辜地将array_values(array_filter())
方法复制/粘贴到他们的代码中——我想知道有多少程序员因为 array_filter 的贪婪而在不知不觉中遇到了问题。或者更糟的是,有多少人从未发现该函数从数组中清除了太多值...
I will present a better alternative for the two-part process of stripping NULL
elements from an array and re-indexing the keys.
对于NULL
从数组中剥离元素和重新索引键的两部分过程,我将提出一个更好的替代方案。
However, first, it is extremely important that I stress the greedy nature of array_filter()
and how this can silently monkeywrench your project. Here is an array with mixed values in it that will expose the trouble:
然而,首先,我要强调贪婪的本质array_filter()
以及这会如何悄悄地破坏您的项目,这一点非常重要。这是一个包含混合值的数组,它将暴露问题:
$array=['foo',NULL,'bar',0,false,null,'0',''];
Null values will be removed regardless of uppercase/lowercase.
无论大写/小写,都将删除空值。
But look at what remains in the array when we use array_values()& array_filter():
但是看看当我们使用array_values()和array_filter()时数组中剩余的内容:
array_values(array_filter($array));
Output array ($array):
输出数组($array):
array (
0 => 'foo',
1 => 'bar'
)
// All empty, zero-ish, falsey values were removed too!!!
Now look at what you get with my method that uses array_walk()& is_null()to generate a new filtered array:
现在看看你用我的方法得到了什么,它使用array_walk()和is_null()来生成一个新的过滤数组:
array_walk($array,function($v)use(&$filtered){if(!is_null($v)){$filtered[]=$v;}});
array_walk($array,function($v)use(&$filtered){if(!is_null($v)){$filtered[]=$v;}});
This can be written over multiple lines for easier reading/explaining:
这可以写成多行以便于阅读/解释:
array_walk( // iterate each element of an input array
$array, // this is the input array
function($v)use(&$filtered){ // $v is each value, $filter (output) is declared/modifiable
if(!is_null($v)){ // this literally checks for null values
$filtered[]=$v; // value is pushed into output with new indexes
}
}
);
Output array ($filter):
输出数组($filter):
array (
0 => 'foo',
1 => 'bar',
2 => 0,
3 => false,
4 => '0',
5 => '',
)
With my method you get your re-indexed keys, all of the non-null values, and none of the null values. A clean, portable, reliable one-liner for all of your array null-filtering needs. Here is a demonstration.
使用我的方法,您可以获得重新索引的键、所有非空值,并且没有空值。一款干净、便携、可靠的单衬管,可满足您所有的阵列空值过滤需求。这是一个演示。
Similarly, if you want to remove empty, false, and null elements (retaining zeros), these four methods will work:
同样,如果要删除空、假和空元素(保留零),这四种方法都可以:
var_export(array_values(array_diff($array,[''])));
or
或者
var_export(array_values(array_diff($array,[null])));
or
或者
var_export(array_values(array_diff($array,[false])));
or
或者
var_export(array_values(array_filter($array,'strlen')));
Output:
输出:
array (
0 => 'foo',
1 => 'bar',
2 => 0,
3 => '0',
)
Finally, for anyone who prefers the syntax of language constructs, you can also just push qualifying values into a new array to issue new indexes.
最后,对于喜欢语言结构语法的任何人,您还可以将符合条件的值推送到新数组中以发布新索引。
$array=['foo', NULL, 'bar', 0, false, null, '0', ''];
$result = [];
foreach ($array as $value) {
if (strlen($value)) {
$result[] = $value;
}
}
var_export($result);
回答by Ed Mazur
Use array_values()
:
使用array_values()
:
<?php
$array = array('foo', 'bar', 'baz');
$array = array_filter($array, function ($var) {
return $var !== 'bar';
});
print_r($array); // indexes 0 and 2
print_r(array_values($array)); // indexes 0 and 1