ItSolutionStuff.com

How to Reset Array Keys in PHP?

By Hardik Savani • May 14, 2024
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: PHP
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to Generate 4,6,8,10 Digit Random number in PHP?

Read Now →

How to Get Last Element of Array in Javascript?

Read Now →

How to Set Value as Key in PHP Array?

Read Now →

How to Convert Array Values to Lowercase in PHP?

Read Now →

How to Convert Object into Array in PHP?

Read Now →

How to Get Maximum Key Value of Array in PHP?

Read Now →

How to Remove Empty Values from Array in PHP?

Read Now →

How to Add Prefix in Each Key of PHP Array?

Read Now →

How to Get Minimum Key Value of Array in PHP?

Read Now →

How to Remove Null Values from Array in PHP?

Read Now →