从数组中删除元素 [英] Delete an element from an array

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

问题描述

有没有一种简单的方法,从一个PHP数组删除元素,这样的foreach($数组)不再包括该元素?

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

我认为它设置为null会做,但显然不是。

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

推荐答案

有不同的方法来删除一个数组元素,其中有些是比其他一些特定的任务更加有用。

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

如果您想删除您可以只使用一个数组元素未设置()或替代 array_splice()

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

此外,如果你有值,不知道键删除您可以使用 <元素code> array_search() 拿到了钥匙。

Also if you have the value and don't know the key to delete the element you can use array_search() to get the key.

请注意,当你使用未设置()数组键不会改变/重新索引。如果你想重新索引键就可以使用 array_values​​() 未设置()将所有键转换为从0开始的数字键枚举

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

code

<?php

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

?>

输出

Array (
    [0] => a
    [2] => c
)

array_splice() 方法

如果您使用 array_splice()键会自动重建索引,但关联键不会改变反对 array_values​​()将所有键转换为数字键。

array_splice() method

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

还有 array_splice()需要的偏移量,不是关键!作为第二个参数。

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

code

<?php

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

?>

输出

Array (
    [0] => a
    [1] => c
)

array_splice()相同未设置()采取通过引用数组,这意味着你不希望到的那些功能恢复的返回值分配给数组

array_splice() same as unset() take the array by reference, this means you don't want to assign the return values of those functions back to the array.

如果你想删除多个数组元素,不想叫未设置() array_splice()多次,你可以使用功能和array_diff() array_diff_key()要删除元素的钥匙。

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 if you know the values or the keys of the elements which you want to delete.

如果您知道您要删除的数组元素的值,那么你可以使用和array_diff()。正如前未设置()它不会改变/重新索引数组的钥匙。

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/reindex the keys of the array.

code

<?php

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

?>

输出

Array (
    [1] => b
)

array_diff_key() 方法

如果您知道要删除元素的钥匙,那么你要使用 array_diff_key()。在这里,你必须确保你通过键作为第二个参数键,而不是值。否则,您必须使用翻转 array_flip()数组。和这里的钥匙也不会改变/重新索引。

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(). Here you have to make sure you pass the keys as keys in the second parameter and not as values. Otherwise you have to flip the array with array_flip(). And also here the keys won't change/reindex.

code

<?php

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

输出

Array (
    [1] => b
)

此外,如果你想使用未设置() array_splice()删除具有相同值的多个元素您可以使用 array_keys() 来让所有的按键为特定的值,然后删除所有元素。

Also 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.

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

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