PHP | Remove element from array with reordering?

19h picture 19h · Jan 31, 2010 · Viewed 8.3k times · Source

How can I remove an element of an array, and reorder afterwards, without having an empty element in the array?

<?php
   $c = array( 0=>12,1=>32 );
   unset($c[0]); // will distort the array.
?>

Answer / Solution: array array_values ( array $input ).

<?php
   $c = array( 0=>12,1=>32 );
   unset($c[0]);
   print_r(array_values($c));
   // will print: the array cleared
?>

Answer

Wim picture Wim · Jan 31, 2010
array_values($c)

will return a new array with just the values, indexed linearly.