w3resource
php Programming Exercies

PHP Array Exercises : Delete an element from an array

PHP Array: Exercise-4 with Solution

$x = array(1, 2, 3, 4, 5);
Delete an element from the above array. After deleting the element, integer keys must be normalized.

Sample Solution:

PHP Code:

<?php
 $x = array(1, 2, 3, 4, 5);
 var_dump($x);
 unset($x[3]);
 $x = array_values($x);
 echo '
';
var_dump($x);
?>

Sample Output:

array(5) {                                                  
  [0]=>                                                     
  int(1)                                                    
  [1]=>                                                     
  int(2)                                                    
  [2]=>                                                     
  int(3)                                                    
  [3]=>                                                     
  int(4)                                                    
  [4]=>                                                     
  int(5)                                                    
}                                                           
array(4) {                                                  
  [0]=>                                                     
  int(1)                                                    
  [1]=>                                                     
  int(2)                                                    
  [2]=> 
  int(3)                                                    
  [3]=>                                                     
  int(5)                                                    
} 

Flowchart:

Flowchart: Delete an element from an array

PHP Code Editor:

Contribute your code and comments through Disqus.

Previous: Create a PHP script which displays the capital and country name from the specified array $ceu. Sort the list by the name of the capital.
Next: Write a PHP script to get the first element of the specified array.

What is the difficulty level of this exercise?



New Content: Composer: Dependency manager for PHP, R Programming