-2

The below code,

$test_array = array("a","b","c","d","e");
echo "<fieldset><pre>";
htmlspecialchars(print_r($test_array));
echo "</pre></fieldset>";

which gives output like,

Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
    [4] => e
)

I want to remove a specific entry say from index 2 and re-index the array as below,

Array
(
    [0] => a
    [1] => b
    [2] => d
    [3] => e
)

How to do that?

Rahul Virpara
  • 1,209
  • 1
  • 12
  • 24

2 Answers2

2

Use array_splice

array_splice($test_array, 2, 1);

The second argument is your index that you want to nix and the third is how many elements you want gone.

NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
Orangepill
  • 24,500
  • 3
  • 42
  • 63
1

Try this

unset($test_array[2]);
$test_array = array_values($test_array);
Nauphal
  • 6,194
  • 4
  • 27
  • 43