Skip to main content
Copy edited (e.g. ref. <http://english.stackexchange.com/questions/4645/is-it-ever-correct-to-have-a-space-before-a-question-or-exclamation-mark#comment206109_4645>).
Source Link
Peter Mortensen
  • 31.5k
  • 22
  • 110
  • 134

Destroy a single element of an array

unset()

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

Out putThe 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 indexingindex the array:

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

Then Outputthe 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

OutputThe 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$color = array("a"=>"red""a" => "red","b"=>"green" "b" => "green" ,"c"=>"blue" "c" => "blue");
$first_color=array_shift$first_color = array_shift($color);
print_r ($color);
print_r ('First Color : '.$first_color);

OutputThe output will be:

Array
(
    [b] => green
    [c] => blue
)
First Color : red

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

Out put 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 indexing array

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

Then 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

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

Output will be

Array
(
    [b] => green
    [c] => blue
)
First Color : red

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
Source Link
KTAnj
  • 1.4k
  • 1
  • 15
  • 36

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

Out put 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 indexing array

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

Then 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

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

Output will be

Array
(
    [b] => green
    [c] => blue
)
First Color : red