php reindex array

Posted by w3diy - 6 months ago

PHP - How to reindex array key after unset key?

<?php

    $myArray = [
        '0' => [
                'name' => 'Ridhi', 
                'email' => '[email protected]',
                'birthdate' => '01/01/1992',
            ], 
        '1' => [
                'name' => 'Rakesh', 
                'email' => '[email protected]',
                'birthdate' => '01/01/1992',
            ], 
        '2' => [
                'name' => 'Rohit', 
                'email' => '[email protected]',
                'birthdate' => '01/01/1992',
            ],
        '3' => [
                'name' => 'Rohit 2', 
                'email' => '[email protected]',
                'birthdate' => '01/01/1992',
            ]
    ];

    unset($myArray[2]);

    $myArray = array_values($myArray);
    print_r($myArray);
?>

Results:

Array
(
    [0] => Array
        (
            [name] => Ridhi
            [email] => [email protected]
            [birthdate] => 01/01/1992
        )
    [1] => Array
        (
            [name] => Rakesh
            [email] => [email protected]
            [birthdate] => 01/01/1992
        )
    [2] => Array
        (
            [name] => Rohit 2
            [email] => [email protected]
            [birthdate] => 01/01/1992
        )
)