How to Reset Array Keys in PHP?

By Hardik Savani May 14, 2024 Category : PHP

Are you looking for example of how to reset array keys in php. you can understand a concept of php reset array keys starting 0. step by step explain php reset array keys to numeric. In this article, we will implement a reset array keys php starting from 0. Let's get started with php array_values in php example.

Here, i will give you two examples of how to reset array keys with php and how to reset keys with remove empty values using array_filter function. so let's see both example with php.

Example 1:

<?php

$myArray = [

'3' => 'Mayank',

'4' => 'Rohit',

'2' => 'Karan',

'7' => 'Himmat'

];

$myNewArray = array_values($myArray);

print_r($myNewArray);

?>

Output

Array

(

[0] => Mayank

[1] => Rohit

[2] => Karan

[3] => Himmat

)

Example 2:

<?php

$myArray = [

'3' => 'Mayank',

'4' => 'Rohit',

'2' => 'Karan',

'7' => 'Himmat',

'6' => '',

'9' => 'Kiran',

'1' => ''

];

$myNewArray = array_values(array_filter($myArray));

print_r($myNewArray);

?>

Output

Array

(

[0] => Mayank

[1] => Rohit

[2] => Karan

[3] => Himmat

[4] => Kiran

)

i hope it can help you...

Tags :
Shares