Deleting an element from an array in PHP

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

    Follow the default functions:

    • PHP: unset

    unset() destroys the specified variables. For more info, you can refer to PHP unset

    $Array = array("test1", "test2", "test3", "test3");
    
    unset($Array[2]);
    
    • PHP: array_pop

    The array_pop() function deletes the last element of an array. For more info, you can refer to PHP array_pop

    $Array = array("test1", "test2", "test3", "test3");
    
    array_pop($Array);
    
    • PHP: array_splice

    The array_splice() function removes selected elements from an array and replaces it with new elements. For more info, you can refer to PHP array_splice

    $Array = array("test1", "test2", "test3", "test3");
    
    array_splice($Array,1,2);
    
    • PHP: array_shift

    The array_shift() function removes the first element from an array. For more info, you can refer to PHP array_shift

    $Array = array("test1", "test2", "test3", "test3");
    
    array_shift($Array);
    

提交回复
热议问题