PHP: Delete an element from an array

2018-12-31 01:10发布

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.

标签: php arrays
30条回答
妖精总统
2楼-- · 2018-12-31 01:34

It should be noted that unset() will keep indexes untouched, which is what you'd expect when using string indexes (array as hashtable), but can be quite surprising when dealing with integer indexed arrays:

$array = array(0, 1, 2, 3);
unset($array[2]);
var_dump($array);
/* array(3) {
  [0]=>
  int(0)
  [1]=>
  int(1)
  [3]=>
  int(3)
} */

$array = array(0, 1, 2, 3);
array_splice($array, 2, 1);
var_dump($array);
/* array(3) {
  [0]=>
  int(0)
  [1]=>
  int(1)
  [2]=>
  int(3)
} */

So array_splice() can be used if you'd like to normalize your integer keys. Another option is using array_values() after unset():

$array = array(0, 1, 2, 3);

unset($array[2]);
$array = array_values($array);
var_dump($array);
/* array(3) {
  [0]=>
  int(0)
  [1]=>
  int(1)
  [2]=>
  int(3)
} */
查看更多
大哥的爱人
3楼-- · 2018-12-31 01:35

Suppose you have such an array:

Array
(
    [user_id] => 193
    [storage] => 5
)

To delete storage, do:

unset($attributes['storage']);
$attributes = array_filter($attributes);

And you get:

Array
(
    [user_id] => 193
)
查看更多
与君花间醉酒
4楼-- · 2018-12-31 01:36
<?php
    $stack = array("fruit1", "fruit2", "fruit3", "fruit4");
    $fruit = array_shift($stack);
    print_r($stack);

    echo $fruit;
?>

Output:

Array
(
    [0] => fruit2
    [1] => fruit3
    [2] => fruit4
)

fruit1
查看更多
梦该遗忘
5楼-- · 2018-12-31 01:36

unset() destroys the specified variables.

The behavior of unset() inside of a function can vary depending on what type of variable you are attempting to destroy.

If a globalized variable is unset() inside of a function, only the local variable is destroyed. The variable in the calling environment will retain the same value as before unset() was called.

<?php
function destroy_foo() 
{
    global $foo;
    unset($foo);
}

$foo = 'bar';
destroy_foo();
echo $foo;
?>

The Answer of the above code will be bar

To unset() a global variable inside of a function

<?php
function foo() 
{
    unset($GLOBALS['bar']);
}

$bar = "something";
foo();
?>
查看更多
长期被迫恋爱
6楼-- · 2018-12-31 01:36

Associative arrays

For associative arrays, use unset :

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

// RESULT : array('a' => 1, 'c' => 3)

Numeric arrays

For numeric arrays, use array_splice :

$arr = array(1, 2, 3);
array_splice($arr, 1, 1);

// RESULT : array(0 => 1, 1 => 3)

Note

Using unset for numeric arrays will not produce an error, but it will mess up your indexes :

$arr = array(1, 2, 3);
unset($arr[1]);

// RESULT : array(0 => 1, 2 => 3)
查看更多
人间绝色
7楼-- · 2018-12-31 01:36

If you need to remove multiple elements from an associative array, you can use array_diff_key() (here used with array_flip()):

$my_array = array(
  "key1" => "value 1",
  "key2" => "value 2",
  "key3" => "value 3",
  "key4" => "value 4",
  "key5" => "value 5",
);

$to_remove = array("key2", "key4");

$result = array_diff_key($my_array, array_flip($to_remove));

print_r($result);

Output:

Array ( [key1] => value 1 [key3] => value 3 [key5] => value 5 ) 
查看更多
登录 后发表回答