Removing Array Element and Re-Indexing in PHP

By IncludeHelp Last updated : February 25, 2024

When working with arrays in PHP, it is essential to efficiently remove elements and re-index the array to maintain a clean and organized structure. In this tutorial, we will explore two popular methods: using unset() and array_values(), as well as introducing array_splice() for more advanced scenarios.

Methods for Removing Array Element and Re-Indexing in PHP

Here are some of the different methods for removing array element and re-indexing in PHP:

  1. unset() and array_values():
    • unset($arr[index]); removes the element at the specified index from the array.
    • $arr = array_values($arr); re-indexes the array, filling any gaps in the indices, ensuring sequential integer keys.
  2. array_splice():
    • array_splice($arr, index, length); removes elements from the array starting at the specified index and for the given length, modifying the original array.
  3. Advanced Technique - array_splice() with replacement:
    • array_splice($arr, index, length, replacement); removes elements from the array at the specified index and for the given length, then inserts replacement elements at that index, effectively replacing the removed portion in the original array.

Now, let's take some examples to get a better idea of these methods.

Using unset() and array_values() in PHP

In this approach, we will use unset() and array_values() methods.

Example

<?php  
// This line of code defines an array
$arr1 = array('Includehelp', 'for', 'Includehelp', 'PHP', 'Programming');

// This line of code removes an item at index 2 ('Includehelp')
unset($arr1[2]);  

//This line of code prints the final array
var_dump($arr1); 

// This line of code is used to re-index the array elements
$arr2 = array_values($arr1); 

// This line of code prints re-indexed array
var_dump($arr2); 
?>

Output

array(4) {
  [0]=>
  string(11) "Includehelp"
  [1]=>
  string(3) "for"
  [3]=>
  string(3) "PHP"
  [4]=>
  string(11) "Programming"
}
array(4) {
  [0]=>
  string(11) "Includehelp"
  [1]=>
  string(3) "for"
  [2]=>
  string(3) "PHP"
  [3]=>
  string(11) "Programming"
}

Explanation

  • We start with an array containing different elements, including "Includehelp."
  • unset($arr1[2]); removes the element at index 2, which is "Includehelp."
  • The modified array is then re-indexed using array_values($arr1).
  • The output shows the array without the removed element and with re-indexed keys.

Using array_splice() Method

In this approach, we will use array_splice() method to remove and re-index array element in PHP.

Example

<?php  
// This line of code define an array
$arr1 = array('Includehelp', 'is', 'awesome', 'for', 'learning');

// This line of code is used to remove item at index 3 ('for')
array_splice($arr1, 3, 1);  

// this line of code finally Prints the modified array
var_dump($arr1); 
?>

Output

array(4) {
  [0]=>
  string(11) "Includehelp"
  [1]=>
  string(2) "is"
  [2]=>
  string(7) "awesome"
  [3]=>
  string(8) "learning"
}

Explanation

  • We start with an array containing various elements, including "Includehelp."
  • array_splice($arr1, 3, 1); removes 1 element starting from index 3, which is "for."
  • The output shows the array without the removed element.

Using array_splice() Method with Replacement

In this we will use an advance technique using array_splice() method with replacement and re-index array element in PHP.

Example

<?php  
// This line of code Defines an array
$arr1 = array('Includehelp', 'for', 'Includehelp');

// This line of code is used to Remove item at 
// index 1 ('for') and then replace with 'PHP'
array_splice($arr1, 1, 1, 'PHP');  

// this line of code finally Prints the modified array
var_dump($arr1); 
?>

Output

array(3) {
  [0]=>
  string(11) "Includehelp"
  [1]=>
  string(3) "PHP"
  [2]=>
  string(11) "Includehelp"
}

Explanation

  • Firstly, an array named $arr1 is defined with three elements: 'Includehelp', 'for', and 'Includehelp'.
  • The array_splice() function is used to modify the array $arr1. It does the following:
  • Removes 1 element starting from index 1 (the second element, 'for').
  • Insert the replacement value 'PHP' at the same index (index 1).
  • Finally, the var_dump() function is used to print the modified array $arr1. The output will show that the element at index 1 has been replaced with 'PHP', resulting in the modified array.

To understand the above examples, you should have the knowledge of the following PHP Topics:

More PHP Array Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.