Hello all! In this article, we will talk about php array remove keys keep values. This tutorial will give you simple example of php reassign array keys. This article goes in detailed on php change array keys to numeric. if you have question about reindex array after unset php then i will give simple example with solution. Alright, let’s dive into the steps.
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:
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...
Do you like below Tutorials ?
- PHP Laravel 5.5 - import export data into excel and csv using maatwebsite
- Laravel 7.x and 6.x create custom facade example from scratch
- Node JS - Get Real time online users counter using socket io example
- PHP Laravel 7.x and 6.x - Bootstrap Admin Theme Integration example from scratch
- MySQL Query - How to select records of today to next Sunday ?
- Laravel 7.x and 6.x - username custom validation example
- Laravel 5.5 - Import Extract zip file using chumper/zipper package
- PHP Laravel Set Custom Validation Error Messages Example