php 如何删除数组元素然后重新索引数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5217721/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 20:40:12  来源:igfitidea点击:

How to Remove Array Element and Then Re-Index Array?

phparraysindexing

提问by daGrevis

I have some troubles with an array. I have one array that I want to modify like below. I want to remove element (elements) of it by index and then re-index array. Is it possible?

我在使用数组时遇到了一些麻烦。我有一个要修改的数组,如下所示。我想通过索引删除它的元素(元素),然后重新索引数组。是否可以?

$foo = array(

    'whatever', // [0]
    'foo', // [1]
    'bar' // [2]

);

$foo2 = array(

    'foo', // [0], before [1]
    'bar' // [1], before [2]

);

回答by xzyfer

unset($foo[0]); // remove item at index 0
$foo2 = array_values($foo); // 'reindex' array

回答by Rene

You better use array_shift(). That will return the first element of the array, remove it from the array and re-index the array. All in one efficient method.

你最好使用array_shift(). 这将返回数组的第一个元素,将其从数组中删除并重新索引数组。一种有效的方法。

回答by user1092222

array_splice($array, array_search(array_value, $array), 1);

回答by frostymarvelous

Unset($array[0]); 

Sort($array); 


I don't know why this is being downvoted, but if anyone has bothered to try it, you will notice that it works. Using sort on an array reassigns the keys of the the array. The only drawback is it sorts the values. Since the keys will obviously be reassigned, even with array_values, it does not matter is the values are being sorted or not.

我不知道为什么这会被否决,但是如果有人愿意尝试一下,您会发现它有效。对数组使用 sort 会重新分配数组的键。唯一的缺点是它对值进行排序。由于键显然会被重新分配,即使使用array_values,值是否被排序都无关紧要。

回答by hsz

Try with:

尝试:

$foo2 = array_slice($foo, 1);

回答by RafaSashi

In addition to xzyfer's answer

除了xzyfer的回答

The function

功能

function custom_unset(&$array=array(), $key=0) {
    if(isset($array[$key])){

        // remove item at index
        unset($array[$key]);

        // 'reindex' array
        $array = array_values($array);

        //alternatively
        //$array = array_merge($array); 

    }
    return $array;
}

Use

$my_array=array(
    0=>'test0', 
    1=>'test1', 
    2=>'test2'
);

custom_unset($my_array, 1);

Result

结果

 array(2) {
    [0]=>
    string(5) "test0"
    [1]=>
    string(5) "test2"
  }

回答by Richard Skinner

If you use array_merge, this will reindex the keys. The manual states:

如果您使用array_merge,这将重新索引键。该手册指出:

Values in the input array with numeric keys will be renumbered with incrementing keys starting from zero in the result array.

带有数字键的输入数组中的值将在结果数组中使用从零开始的递增键重新编号。

http://php.net/manual/en/function.array-merge.php

http://php.net/manual/en/function.array-merge.php

This is where i found the original answer.

这是我找到原始答案的地方。

http://board.phpbuilder.com/showthread.php?10299961-Reset-index-on-array-after-unset()

http://board.phpbuilder.com/showthread.php?10299961-Reset-index-on-array-after-unset()