在PHP中删除数组项的最佳方式是什么?

时间:2022-11-24 16:00:37

Could you tell me your way to delete an item from array? Do you think it's good?

你能告诉我怎么从数组中删除一个条目吗?你觉得这样好吗?

3 个解决方案

#1


9  

That depends:

这取决于:

$a1 = array('a' => 1, 'b' => 2, 'c' => 3);
unset($a1['b']);
// array('a' => 1, 'c' => 3)

$a2 = array(1, 2, 3);
unset($a2[1]);
// array(0 => 1, 2 => 3)
// note the missing index 1

// solution 1 for numeric arrays
$a3 = array(1, 2, 3);
array_splice($a3, 1, 1);
// array(0 => 1, 1 => 3)
// index is now continous

// solution 2 for numeric arrays
$a4 = array(1, 2, 3);
unset($a4[1]);
$a4 = array_values($a4);
// array(0 => 1, 1 => 3)
// index is now continous

Generally unset() is safe for hashtables (string-indexed arrays), but if you have to rely on continous numeric indexes you'll have to use either array_splice() or a combination of unset() and array_values().

一般来说,unset()对于散列表(字符串索引数组)是安全的,但是如果必须依赖连续的数字索引,则必须使用array_splice()或unset()和array_values()的组合。

#2


9  

The common way:

常用方法:

According to the manual

根据手册

unset($arr[5]); // This removes the element from the array

The filtered way:

过滤的方法:

There is also the array_filter() function to take care of filtering arrays

还有array_filter()函数来处理过滤数组

$numeric_data = array_filter($data, "is_numeric");

To get a sequential index you can use

要获得一个可以使用的顺序索引

$numeric_data = array_values($numeric_data);

References
PHP – Delete selected items from an array

引用PHP -从数组中删除选定的项。

#3


5  

It depends. If want to remove an element without causing gaps in the indexes, you need to use array_splice:

视情况而定。如果要删除一个元素而不造成索引中的空白,需要使用array_splice:

$a = array('a','b','c', 'd');
array_splice($a, 2, 1);
var_dump($a);

Output:

输出:

array(3) {
  [0]=>
  string(1) "a"
  [1]=>
  string(1) "b"
  [2]=>
  string(1) "d"
}

Using unset can work, but this results in a non-continuous index. This can sometimes be a problem when you iterate over the array using count($a) - 1 as a measure of the upper bound:

使用unset可以工作,但是这会导致一个非连续索引。当您使用count($a) - 1作为上界的度量来迭代数组时,这有时会成为一个问题:

$a = array('a','b','c', 'd');
unset($a[2]);
var_dump($a);

Output:

输出:

array(3) {
  [0]=>
  string(1) "a"
  [1]=>
  string(1) "b"
  [3]=>
  string(1) "d"
}

As you see, count is now 3 but the index of the last element is also 3.

如您所见,count现在是3,但是最后一个元素的索引也是3。

My recommendation is therefore to use array_splice for arrays with numerical indexes, and use unset only for arrays (dictionaries really) with non-numerical indexes.

因此,我建议对具有数值索引的数组使用array_splice,并只对具有非数值索引的数组(实际上是字典)使用unset。

#1


9  

That depends:

这取决于:

$a1 = array('a' => 1, 'b' => 2, 'c' => 3);
unset($a1['b']);
// array('a' => 1, 'c' => 3)

$a2 = array(1, 2, 3);
unset($a2[1]);
// array(0 => 1, 2 => 3)
// note the missing index 1

// solution 1 for numeric arrays
$a3 = array(1, 2, 3);
array_splice($a3, 1, 1);
// array(0 => 1, 1 => 3)
// index is now continous

// solution 2 for numeric arrays
$a4 = array(1, 2, 3);
unset($a4[1]);
$a4 = array_values($a4);
// array(0 => 1, 1 => 3)
// index is now continous

Generally unset() is safe for hashtables (string-indexed arrays), but if you have to rely on continous numeric indexes you'll have to use either array_splice() or a combination of unset() and array_values().

一般来说,unset()对于散列表(字符串索引数组)是安全的,但是如果必须依赖连续的数字索引,则必须使用array_splice()或unset()和array_values()的组合。

#2


9  

The common way:

常用方法:

According to the manual

根据手册

unset($arr[5]); // This removes the element from the array

The filtered way:

过滤的方法:

There is also the array_filter() function to take care of filtering arrays

还有array_filter()函数来处理过滤数组

$numeric_data = array_filter($data, "is_numeric");

To get a sequential index you can use

要获得一个可以使用的顺序索引

$numeric_data = array_values($numeric_data);

References
PHP – Delete selected items from an array

引用PHP -从数组中删除选定的项。

#3


5  

It depends. If want to remove an element without causing gaps in the indexes, you need to use array_splice:

视情况而定。如果要删除一个元素而不造成索引中的空白,需要使用array_splice:

$a = array('a','b','c', 'd');
array_splice($a, 2, 1);
var_dump($a);

Output:

输出:

array(3) {
  [0]=>
  string(1) "a"
  [1]=>
  string(1) "b"
  [2]=>
  string(1) "d"
}

Using unset can work, but this results in a non-continuous index. This can sometimes be a problem when you iterate over the array using count($a) - 1 as a measure of the upper bound:

使用unset可以工作,但是这会导致一个非连续索引。当您使用count($a) - 1作为上界的度量来迭代数组时,这有时会成为一个问题:

$a = array('a','b','c', 'd');
unset($a[2]);
var_dump($a);

Output:

输出:

array(3) {
  [0]=>
  string(1) "a"
  [1]=>
  string(1) "b"
  [3]=>
  string(1) "d"
}

As you see, count is now 3 but the index of the last element is also 3.

如您所见,count现在是3,但是最后一个元素的索引也是3。

My recommendation is therefore to use array_splice for arrays with numerical indexes, and use unset only for arrays (dictionaries really) with non-numerical indexes.

因此,我建议对具有数值索引的数组使用array_splice,并只对具有非数值索引的数组(实际上是字典)使用unset。