php 取消设置元素后重新设置数组键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5943149/
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
Rebase array keys after unsetting elements
提问by TuK
I have an array:
我有一个数组:
$array = array(1,2,3,4,5);
If I were to dump the contents of the array they would look like this:
如果我要转储数组的内容,它们将如下所示:
array(5) {
[0] => int(1)
[1] => int(2)
[2] => int(3)
[3] => int(4)
[4] => int(5)
}
When I loop through and unset certain keys, the index gets all Hymaned up.
当我循环并取消设置某些键时,索引会全部被提升。
foreach($array as $i => $info)
{
if($info == 1 || $info == 2)
{
unset($array[$i]);
}
}
Subsequently, if I did another dump now it would look like:
随后,如果我现在再做一次转储,它看起来像:
array(3) {
[2] => int(3)
[3] => int(4)
[4] => int(5)
}
Is there a proper way to reset the array so it's elements are Zero based again ??
是否有正确的方法来重置数组,使其元素再次基于零??
array(3) {
[0] => int(3)
[1] => int(4)
[2] => int(5)
}
回答by Naftali aka Neal
回答by Web_Developer
Got another interesting method:
还有一个有趣的方法:
$array = array('a', 'b', 'c', 'd');
unset($array[2]);
$array = array_merge($array);
Now the $array keys are reset.
现在 $array 键被重置。
回答by Demian Brecht
Use array_splice
rather than unset
:
使用array_splice
而不是unset
:
$array = array(1,2,3,4,5);
foreach($array as $i => $info)
{
if($info == 1 || $info == 2)
{
array_splice($array, $i, 1);
}
}
print_r($array);
回答by SpYk3HH
Just an additive.
只是一种添加剂。
I know this is old, but I wanted to add a solution I don't see that I came up with myself. Found this question while on hunt of a different solution and just figured, "Well, while I'm here."
我知道这很旧,但我想添加一个我自己没有想到的解决方案。在寻找不同的解决方案时发现了这个问题,只是想,“好吧,当我在这里的时候。”
First of all, Neal's answer is good and great to use after you run your loop, however, I'd prefer do all work at once. Of course, in my specific case I had to do morework than this simple example here, but the method still applies. I saw where a couple others suggested foreach
loops, however, this still leaves you with after workdue to the nature of the beast. Normally I suggest simpler things like foreach
, however, in this case, it's best to remember good old fashioned for loop
logic. Simply use i
! To maintain appropriate index, just subtract from i
after each removal of an Array item.
首先,Neal的答案很好,在您运行循环后非常适合使用,但是,我更喜欢一次完成所有工作。当然,在我的特定情况下,我必须做比这里的这个简单示例更多的工作,但该方法仍然适用。我看到其他几个人建议foreach
循环的地方,但是,由于野兽的性质,这仍然让你在下班后留下来。通常我建议使用更简单的东西foreach
,但是,在这种情况下,最好记住好的老式for loop
逻辑。只需使用i
!要保持适当的索引,只需i
在每次删除 Array 项后减去。
Here's my simple, workingexample:
这是我的简单工作示例:
$array = array(1,2,3,4,5);
for ($i = 0; $i < count($array); $i++) {
if($array[$i] == 1 || $array[$i] == 2) {
array_splice($array, $i, 1);
$i--;
}
}
Will output:
将输出:
array(3) {
[0]=> int(3)
[1]=> int(4)
[2]=> int(5)
}
This can have many simple implementations. For example, my exact case required holding of latest item in array based on multidimensional values. I'll show you what I mean:
这可以有许多简单的实现。例如,我的确切情况需要根据多维值在数组中保存最新项目。我会告诉你我的意思:
$files = array(
array(
'name' => 'example.zip',
'size' => '100000000',
'type' => 'application/x-zip-compressed',
'url' => '28188b90db990f5c5f75eb960a643b96/example.zip',
'deleteUrl' => 'server/php/?file=example.zip',
'deleteType' => 'DELETE'
),
array(
'name' => 'example.zip',
'size' => '10726556',
'type' => 'application/x-zip-compressed',
'url' => '28188b90db990f5c5f75eb960a643b96/example.zip',
'deleteUrl' => 'server/php/?file=example.zip',
'deleteType' => 'DELETE'
),
array(
'name' => 'example.zip',
'size' => '110726556',
'type' => 'application/x-zip-compressed',
'deleteUrl' => 'server/php/?file=example.zip',
'deleteType' => 'DELETE'
),
array(
'name' => 'example2.zip',
'size' => '12356556',
'type' => 'application/x-zip-compressed',
'url' => '28188b90db990f5c5f75eb960a643b96/example2.zip',
'deleteUrl' => 'server/php/?file=example2.zip',
'deleteType' => 'DELETE'
)
);
for ($i = 0; $i < count($files); $i++) {
if ($i > 0) {
if (is_array($files[$i-1])) {
if (!key_exists('name', array_diff($files[$i], $files[$i-1]))) {
if (!key_exists('url', $files[$i]) && key_exists('url', $files[$i-1])) $files[$i]['url'] = $files[$i-1]['url'];
$i--;
array_splice($files, $i, 1);
}
}
}
}
Will output:
将输出:
array(1) {
[0]=> array(6) {
["name"]=> string(11) "example.zip"
["size"]=> string(9) "110726556"
["type"]=> string(28) "application/x-zip-compressed"
["deleteUrl"]=> string(28) "server/php/?file=example.zip"
["deleteType"]=> string(6) "DELETE"
["url"]=> string(44) "28188b90db990f5c5f75eb960a643b96/example.zip"
}
[1]=> array(6) {
["name"]=> string(11) "example2.zip"
["size"]=> string(9) "12356556"
["type"]=> string(28) "application/x-zip-compressed"
["deleteUrl"]=> string(28) "server/php/?file=example2.zip"
["deleteType"]=> string(6) "DELETE"
["url"]=> string(45) "28188b90db990f5c5f75eb960a643b96/example2.zip"
}
}
As you see, I manipulate $i before the splice as I'm seeking to remove the previous, rather than the present item.
如您所见,我在拼接之前操作 $i ,因为我试图删除前一项,而不是当前项。
回答by upful
Or you can make your own function that passes the array by reference.
或者,您可以创建自己的函数,通过引用传递数组。
function array_unset($unsets, &$array) {
foreach ($array as $key => $value) {
foreach ($unsets as $unset) {
if ($value == $unset) {
unset($array[$key]);
break;
}
}
}
$array = array_values($array);
}
So then all you have to do is...
那么你所要做的就是......
$unsets = array(1,2);
array_unset($unsets, $array);
... and now your $array
is without the values you placed in $unsets
and the keys are reset
......现在你$array
没有你放置的值$unsets
并且键被重置
回答by K-Gun
Late answer but, after PHP 5.3 could be so;
迟到的答案但是,在 PHP 5.3 之后可能是这样;
$array = array(1, 2, 3, 4, 5);
$array = array_values(array_filter($array, function($v) {
return !($v == 1 || $v == 2);
}));
print_r($array);
回答by Atta Ur Rehman
100% working for me ! After unset elements in array you can use this for re-indexing the array
100% 为我工作!在数组中取消设置元素后,您可以使用它来重新索引数组
$result=array_combine(range(1, count($your_array)), array_values($your_array));
回答by Rockin4Life33
I use $arr = array_merge($arr);
to rebase an array. Simple and straightforward.
我用来 $arr = array_merge($arr);
重新定义一个数组。简单明了。
回答by John K
In my situation, I needed to retain unique keys with the array values, so I just used a second array:
在我的情况下,我需要使用数组值保留唯一键,所以我只使用了第二个数组:
$arr1 = array("alpha"=>"bravo","charlie"=>"delta","echo"=>"foxtrot");
unset($arr1);
$arr2 = array();
foreach($arr1 as $key=>$value) $arr2[$key] = $value;
$arr1 = $arr2
unset($arr2);