+

Removing an array element and re-indexing in PHP

Function to use:
  • unset():this function unset this variable. 
    Syntax:
    void unset (mixed $var [, mixed $...]) 
  • array_values ​​(): this function returns all values ​​from an array and numerically indexes the array. 
    Syntax:
    array array_values ​​(array $array) 
  • Example 1: $arr1 = array ( `engineer` , // [0] `for` , // [1] `engineer` // [2] 
    );  
    // remove the element with index 1, which is intended for unset ( $arr1 [1]);  
    // Print the modified array var_dump ( $arr1 );  
    // Re-index array elements $arr2 = array_values ​​ ( $arr1 );  
    // Print the reindexed array var_dump ( $arr1 ); ?>
    Exit:
    array (2 ) {[0] = > string (5) "engineer" [2] = > string (5) "engineer"} array (2) {[0] = > string (5) "engineer" [2] = > string (5) "engineer"} 
    We can also use the array_splice() function which removes some array and replaces it with something else. 
    Example 2: $arr1 = array ( `engineer` , // [0] `for` , // [1] `engineer` // [2]);  
    // remove the element with index 1, which is intended for array_splice ( $arr1 , 1, 1);  
    // Print the modified array var_dump ( $arr1 ); ?>
    Exit:
    array (2 ) {[0] = > string (5) "engineer" [1] = > string (5) "engineer"} 
    Get Solution for free from DataCamp guru