Ad Code



Delete element from array in php

Delete element from array in php

If you want to delete single elements from an array you can use simply unset().

Using unset()

$array = array('A', 'B', 'C', 'D');
unset($array[1]);    //enter index no which element want to delete

Array (
    [0] => A
    [2] => C
    [3] => D
//After delete elements array print and in this not have B element because it s deleted

Using array_splice()

Another method of deleting array elements is array_splice(), in which this key automatically will be re-indexed. If you are using an associative array then you use array_values() which convert all keys into numerical keys.

$array = array('A', 'B', 'C', 'D');
array_splice($array, 1, 1);
                  // ↑ enter offset no of the which want to delete

Post a Comment


Ad Code