PHP – How to reindex array key after unset key?

PHP – How to reindex array key after unset key?

In this post we will give you information about PHP – How to reindex array key after unset key?. Hear we will give you detail about PHP – How to reindex array key after unset key?And how to use it also give you demo for it if it is necessary.

Today, we will learn how to reindex array key from 0 after unset key. we can reassign key using array_values function of php. i will give you simple example for reindex array from 0 after unset key using array_values function. we will reassign keys from numeric like 0 1 2 etc.

Sometime you need to store json to database column at that time it is better if you keep array key as numeric with 0 1 2 etc. so you can easily do json encode and json decode. so you can see in this example how you can make it done for reassign array keys in php.

Example:

<?php

$myArray = [

'0' => [

'name' => 'Paresh',

'email' => 'paresh@gmail.com',

'birthdate' => '01/01/1990',

],

'1' => [

'name' => 'Rakesh',

'email' => 'rakesh@gmail.com',

'birthdate' => '01/01/1990',

],

'2' => [

'name' => 'Mahesh',

'email' => 'mahesh@gmail.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:

Also see:How to convert Object into Array in PHP?

Array

(

[0] => Array

(

[name] => Paresh

[email] => paresh@gmail.com

[birthdate] => 01/01/1990

)

[1] => Array

(

[name] => Rakesh

[email] => rakesh@gmail.com

[birthdate] => 01/01/1990

)

[2] => Array

(

[name] => Mahesh 2

[email] => mahesh@gmail.com

[birthdate] => 01/01/1990

)

)

I hope it can help you…

Hope this code and post will helped you for implement PHP – How to reindex array key after unset key?. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

  +  30  =  36

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US