PHP 删除数组中的元素 [英] Deleting an element from an array in PHP

查看:38
本文介绍了PHP 删除数组中的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种简单的方法可以使用 PHP 从数组中删除元素,使得 foreach ($array) 不再包含该元素?

我认为将它设置为 null 可以做到,但显然它不起作用.

解决方案

删除数组元素的方法有很多种,其中一些方法对于某些特定任务比其他方法更有用.

删除单个数组元素

如果你只想删除一个数组元素,你可以使用 unset()\array_splice().

如果您知道值但不知道删除元素的键,您可以使用 \array_search() 获取密钥.这仅在元素出现多次时才有效,因为 \array_search 仅返回第一个命中.

unset()

请注意,当您使用 unset() 时,数组键不会改变.如果要重新索引键,可以使用 \array_values() unset() 之后,它将所有键转换为从 0 开始的数字枚举键.

代码:

$array = [0 =>a",1=>b",2=>c"];未设置($array[1]);//↑ 要删除的键

输出:

<预><代码>[[0] =>一种[2] =>C]

\array_splice() 方法

如果你使用 \array_splice() 键将自动重新索引,但关联键不会改变——与 \array_values() 相反,它会将所有键转换为数字键.

\array_splice() 需要 offset 而不是 key 作为第二个参数.

代码:

$array = [0 =>a",1=>b",2=>c"];\array_splice($array, 1, 1);//↑ 要删除的偏移量

输出:

<预><代码>[[0] =>一种[1] =>C]

array_splice()unset() 相同,通过引用获取数组.您无需将这些函数的返回值分配回数组.

删除多个数组元素

如果你想删除多个数组元素并且不想多次调用unset()\array_splice()你可以使用函数\array_diff()\array_diff_key() 取决于您是否知道要删除的元素的值或键.

\array_diff() 方法

如果您知道要删除的数组元素的值,则可以使用\array_diff().和之前的 unset() 一样,它不会改变数组的键.

代码:

$array = [0 =>a",1=>b",2=>c",3=>c"];$array = \array_diff($array, [a", c"]);//└──────────┘//要删除的数组值

输出:

<预><代码>[[1] =>乙]

\array_diff_key() 方法

如果您知道要删除的元素的键,那么您想使用\array_diff_key().您必须确保将键作为第二个参数中的键而不是作为值传递.键不会重新索引.

代码:

$array = [0 =>a",1=>b",2=>c"];$array = \array_diff_key($array, [0 => "xy", "2" => "xy"]);//↑ ↑//要删除的数组键

输出:

<预><代码>[[1] =>乙]

如果你想使用 unset()\array_splice() 删除多个具有相同值的元素,你可以使用 \array_keys() 获取特定值的所有键,然后删除所有元素.

Is there an easy way to delete an element from an array using PHP, such that foreach ($array) no longer includes that element?

I thought that setting it to null would do it, but apparently it does not work.

解决方案

There are different ways to delete an array element, where some are more useful for some specific tasks than others.

Deleting a single array element

If you want to delete just one array element you can use unset() or alternatively \array_splice().

If you know the value and don’t know the key to delete the element you can use \array_search() to get the key. This only works if the element does not occur more than once, since \array_search returns the first hit only.

unset()

Note that when you use unset() the array keys won’t change. If you want to reindex the keys you can use \array_values() after unset(), which will convert all keys to numerically enumerated keys starting from 0.

Code:

$array = [0 => "a", 1 => "b", 2 => "c"];
unset($array[1]);
          // ↑ Key which you want to delete

Output:

[
    [0] => a
    [2] => c
]

\array_splice() method

If you use \array_splice() the keys will automatically be reindexed, but the associative keys won’t change — as opposed to \array_values(), which will convert all keys to numerical keys.

\array_splice() needs the offset, not the key, as the second parameter.

Code:

$array = [0 => "a", 1 => "b", 2 => "c"];
\array_splice($array, 1, 1);
                   // ↑ Offset which you want to delete

Output:

[
    [0] => a
    [1] => c
]

array_splice(), same as unset(), take the array by reference. You don’t assign the return values of those functions back to the array.

Deleting multiple array elements

If you want to delete multiple array elements and don’t want to call unset() or \array_splice() multiple times you can use the functions \array_diff() or \array_diff_key() depending on whether you know the values or the keys of the elements which you want to delete.

\array_diff() method

If you know the values of the array elements which you want to delete, then you can use \array_diff(). As before with unset() it won’t change the keys of the array.

Code:

$array = [0 => "a", 1 => "b", 2 => "c", 3 => "c"];
$array = \array_diff($array, ["a", "c"]);
                          // └────────┘
                          // Array values which you want to delete

Output:

[
    [1] => b
]

\array_diff_key() method

If you know the keys of the elements which you want to delete, then you want to use \array_diff_key(). You have to make sure you pass the keys as keys in the second parameter and not as values. Keys won’t reindex.

Code:

$array = [0 => "a", 1 => "b", 2 => "c"];
$array = \array_diff_key($array, [0 => "xy", "2" => "xy"]);
                               // ↑           ↑
                               // Array keys which you want to delete

Output:

[
    [1] => b
]

If you want to use unset() or \array_splice() to delete multiple elements with the same value you can use \array_keys() to get all the keys for a specific value and then delete all elements.

这篇关于PHP 删除数组中的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆