In this tutorial, We will learn how to re-index the array key from 0 after the unset key in PHP.


Example:

<?php
   
    $myArray = [
        '0' => [
                'name' => 'Jhon', 
                'email' => 'jhon@mail.com',
                'birthdate' => '01/01/1990',
            ], 
        '1' => [
                'name' => 'rathrji', 
                'email' => 'rathorji@mail.com',
                'birthdate' => '01/01/1990',
            ], 
        '2' => [
                'name' => 'jack', 
                'email' => 'jack@mail.com',
                'birthdate' => '01/01/1990',
            ],
        '3' => [
                'name' => 'Mahesh 2', 
                'email' => 'mahesh@gmail.com',
                'birthdate' => '01/01/1990',
            ]
    ];
   
    unset($myArray[2]);
   
    $myArray = array_values($myArray);
    
    print_r($myArray);
?>


output:

Array
(
    [0] => Array
        (
            [name] => Jhon
            [email] => jhon@mail.com
            [birthdate] => 01/01/1990
        )
    [1] => Array
        (
            [name] => rathrji
            [email] => rathorji@mail.com
            [birthdate] => 01/01/1990
        )
    [2] => Array
        (
            [name] => Mahesh 2
            [email] => mahesh@gmail.com
            [birthdate] => 01/01/1990
        )
)