Deleting an element from an array in PHP

后端 未结 30 3353
时光说笑
时光说笑 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:24

    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)
    } */
    
    0 讨论(0)
  • 2020-11-21 06:24

    Destroy a single element of an array

    unset()

    $array1 = array('A', 'B', 'C', 'D', 'E');
    unset($array1[2]); // Delete known index(2) value from array
    var_dump($array1);
    

    The output will be:

    array(4) {
      [0]=>
      string(1) "A"
      [1]=>
      string(1) "B"
      [3]=>
      string(1) "D"
      [4]=>
      string(1) "E"
    }
    

    If you need to re index the array:

    $array1 = array_values($array1);
    var_dump($array1);
    

    Then the output will be:

    array(4) {
      [0]=>
      string(1) "A"
      [1]=>
      string(1) "B"
      [2]=>
      string(1) "D"
      [3]=>
      string(1) "E"
    }
    

    Pop the element off the end of array - return the value of the removed element

    mixed array_pop(array &$array)

    $stack = array("orange", "banana", "apple", "raspberry");
    $last_fruit = array_pop($stack);
    print_r($stack);
    print_r('Last Fruit:'.$last_fruit); // Last element of the array
    

    The output will be

    Array
    (
        [0] => orange
        [1] => banana
        [2] => apple
    )
    Last Fruit: raspberry
    

    Remove the first element (red) from an array, - return the value of the removed element

    mixed array_shift ( array &$array )

    $color = array("a" => "red", "b" => "green" , "c" => "blue");
    $first_color = array_shift($color);
    print_r ($color);
    print_r ('First Color: '.$first_color);
    

    The output will be:

    Array
    (
        [b] => green
        [c] => blue
    )
    First Color: red
    
    0 讨论(0)
  • 2020-11-21 06:24

    Use array_search to get the key and remove it with unset if found:

    if (($key = array_search('word', $array)) !== false) {
        unset($array[$key]);
    }
    
    0 讨论(0)
  • 2020-11-21 06:24

    Remove an array element based on a key:

    Use the unset function like below:

    $a = array(
           'salam',
           '10',
           1
    );
    
    unset($a[1]);
    
    print_r($a);
    
    /*
    
        Output:
    
            Array
            (
                [0] => salam
                [2] => 1
            )
    
    */
    

    Remove an array element based on value:

    Use the array_search function to get an element key and use the above manner to remove an array element like below:

    $a = array(
           'salam',
           '10',
           1
    );
    
    $key = array_search(10, $a);
    
    if ($key !== false) {
        unset($a[$key]);
    }
    
    print_r($a);
    
    /*
    
        Output:
    
            Array
            (
                [0] => salam
                [2] => 1
            )
    
    */
    
    0 讨论(0)
  • 2020-11-21 06:25

    Two ways for removing the first item of an array with keeping order of the index and also if you don't know the key name of the first item.

    Solution #1

    // 1 is the index of the first object to get
    // NULL to get everything until the end
    // true to preserve keys
    $array = array_slice($array, 1, null, true);
    

    Solution #2

    // Rewinds the array's internal pointer to the first element
    // and returns the value of the first array element.
    $value = reset($array);
    // Returns the index element of the current array position
    $key = key($array);
    unset($array[$key]);
    

    For this sample data:

    $array = array(10 => "a", 20 => "b", 30 => "c");
    

    You must have this result:

    array(2) {
      [20]=>
      string(1) "b"
      [30]=>
      string(1) "c"
    }
    
    0 讨论(0)
  • 2020-11-21 06:26

    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);
    ?>
    

    The result will be:

    Array ( [0] => purple [1] => orange [c] => blue [d] => yellow )
    
    0 讨论(0)
提交回复
热议问题