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:38

This may help...

<?php
    $a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
    $a2=array("a"=>"purple","b"=>"orange");
    array_splice($a1,0,2,$a2);
    print_r($a1);
    ?>

result will be:

Array ( [0] => purple [1] => orange [c] => blue [d] => yellow )
查看更多
大哥的爱人
3楼-- · 2018-12-31 01:39

Also, for a named element:

unset($array["elementName"]);
查看更多
无色无味的生活
4楼-- · 2018-12-31 01:39
// Remove by value
function removeFromArr($arr, $val)
{
    unset($arr[array_search($val, $arr)]);
    return array_values($arr);
}
查看更多
路过你的时光
5楼-- · 2018-12-31 01:41
$arr = array('orange', 'banana', 'apple', 'raspberry');
$result= array_pop($arr);
print_r($result);
查看更多
美炸的是我
6楼-- · 2018-12-31 01:41

unset don't change the index but array_splice does

$arrayName = array( '1' => 'somevalue',
                        '2' => 'somevalue1',
                        '3' => 'somevalue3',
                        500 => 'somevalue500',
                             );


    echo $arrayName['500']; 
    //somevalue500
    array_splice($arrayName, 1,2);

    print_r( $arrayName );
    //Array ( [0] => somevalue [1] => somevalue500 )



    $arrayName = array( '1' => 'somevalue',
                        '2' => 'somevalue1',
                        '3' => 'somevalue3',
                        500 => 'somevalue500',
                             );


    echo $arrayName['500']; 
    //somevalue500
    unset($arrayName[1]);

    print_r( $arrayName );
    //Array ( [0] => somevalue [1] => somevalue500 )
查看更多
伤终究还是伤i
7楼-- · 2018-12-31 01:42

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

Delete one array element

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

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.

unset() method

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

?>

Output

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

array_splice() method

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

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

Code

<?php

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

?>

Output

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

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.

Delete 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 if 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/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

?>

Output

Array (
    [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(). 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
?>

Output

Array (
    [1] => b
)

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.

查看更多
登录 后发表回答