Deleting an element from an array in PHP

后端 未结 30 3356
时光说笑
时光说笑 2020-11-21 05:55

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

相关标签:
30条回答
  • 2020-11-21 06:39
    <?php
        $stack = ["fruit1", "fruit2", "fruit3", "fruit4"];
        $fruit = array_shift($stack);
        print_r($stack);
    
        echo $fruit;
    ?>
    

    Output:

    [
        [0] => fruit2
        [1] => fruit3
        [2] => fruit4
    ]
    
    fruit1
    
    0 讨论(0)
  • 2020-11-21 06:40
    <?php
        // If you want to remove a particular array element use this method
        $my_array = array("key1"=>"value 1", "key2"=>"value 2", "key3"=>"value 3");
    
        print_r($my_array);
        if (array_key_exists("key1", $my_array)) {
            unset($my_array['key1']);
            print_r($my_array);
        }
        else {
            echo "Key does not exist";
        }
    ?>
    
    <?php
        //To remove first array element
        $my_array = array("key1"=>"value 1", "key2"=>"value 2", "key3"=>"value 3");
        print_r($my_array);
        $new_array = array_slice($my_array, 1);
        print_r($new_array);
    ?>
    
    
    <?php
        echo "<br/>    ";
        // To remove first array element to length
        // starts from first and remove two element
        $my_array = array("key1"=>"value 1", "key2"=>"value 2", "key3"=>"value 3");
        print_r($my_array);
        $new_array = array_slice($my_array, 1, 2);
        print_r($new_array);
    ?>
    

    Output

     Array ( [key1] => value 1 [key2] => value 2 [key3] =>
     value 3 ) Array (    [key2] => value 2 [key3] => value 3 )
     Array ( [key1] => value 1 [key2] => value 2 [key3] => value 3 )
     Array ( [key2] => value 2 [key3] => value 3 )
     Array ( [key1] => value 1 [key2] => value 2 [key3] => value 3 )
     Array ( [key2] => value 2 [key3] => value 3 )
    
    0 讨论(0)
  • 2020-11-21 06:40
    $arrayName = array( '1' => 'somevalue',
                        '2' => 'somevalue1',
                        '3' => 'somevalue3',
                      );
    
    print_r($arrayName[1]);
    // somevalue
    unset($arrayName[1]);
    
    print_r($arrayName);
    
    0 讨论(0)
  • 2020-11-21 06:44

    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)
    
    0 讨论(0)
  • 2020-11-21 06:46

    If you have a numerically indexed array where all values are unique (or they are non-unique but you wish to remove all instances of a particular value), you can simply use array_diff() to remove a matching element, like this:

    $my_array = array_diff($my_array, array('Value_to_remove'));
    

    For example:

    $my_array = array('Andy', 'Bertha', 'Charles', 'Diana');
    echo sizeof($my_array) . "\n";
    $my_array = array_diff($my_array, array('Charles'));
    echo sizeof($my_array);
    

    This displays the following:

    4
    3
    

    In this example, the element with the value 'Charles' is removed as can be verified by the sizeof() calls that report a size of 4 for the initial array, and 3 after the removal.

    0 讨论(0)
  • 2020-11-21 06:47
    $key = array_search($needle, $array);
    if ($key !== false) {
        unset($array[$key]);
    }
    
    0 讨论(0)
提交回复
热议问题