Search code examples
phparrayspointersreturnunset

PHP | Remove element from array with reordering?


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
?>

Solution

  • array_values($c)
    

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