Hi, I want to remove one element from array so i'm using unset function but it also remove that index number. I want to re-index after removing array. is there any solution?
remove element from array
Collapse
X
-
Try the array_splice() Function. In the following example, I remove 'black' from the Original Array and the Elements have appered to have been Re-indexed after the splice.
Code:<?php $colors = Array("red", "green", "Blue", "purple", "orange", "black", "magenta", "cyan", "yellow", "violet", "brown"); echo 'Number of Elements in $colors: ' . count($colors); echo "<BR><BR>"; $colors_removed = array_splice($colors, 5, 1); echo 'Number of Elements in $colors_removed: ' . count($colors_removed) . "<BR>"; echo $colors[0] . "<BR>"; echo $colors[1] . "<BR>"; echo $colors[2] . "<BR>"; echo $colors[3] . "<BR>"; echo $colors[4] . "<BR>"; echo $colors[5] . "<BR>"; echo $colors[6] . "<BR>"; echo $colors[7] . "<BR>"; echo $colors[8] . "<BR>"; echo $colors[9] . "<BR>";
Code:Number of Elements in $colors: 11 Number of Elements in $colors_removed: 1 red green Blue purple orange magenta cyan yellow violet brown
Comment
Comment