ItSolutionStuff.com

How to Remove Numeric Keys in PHP Array?

By Hardik Savani • May 14, 2024
PHP

Hi,

I am going to show you an example of php array remove numeric keys. you can understand a concept of how to remove numeric keys in array php. This example will help you how to remove numeric keys in php array. I explained simply step by step how to delete number keys from array in php. follow the below example for how to delete numeric keys from array in php.

we will use array_filter() function with is_string and ARRAY_FILTER_USE_KEY to remove numeric keys from php array. so, let's see the simple code of how to delete numeric keys from php array.

Example:

index.php

<?php

$myArray = [1 => "One", "Two" => 2, 3 => "Three", "Four" => 4, 5 => "Five", "Six" => 6];

$newArray = array_filter(

$myArray,

function ($k) { return is_string($k); },

ARRAY_FILTER_USE_KEY

);

var_dump($newArray);

?>

Output:

array(3) {

["Two"]=> int(2)

["Four"]=> int(4)

["Six"]=> int(6)

}

I hope it can help you...

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 Remove Numeric Values in PHP Array?

Read Now →

How to Get First 10 Elements of Array in PHP?

Read Now →

How to Get First 5 Elements of Array in PHP?

Read Now →

How to Get Last 5 Elements of Array in PHP?

Read Now →

How to Get Last 3 Elements of Array in PHP?

Read Now →

How to Get Last 2 Elements of Array in PHP?

Read Now →

How to Get Random Value from an Array in PHP?

Read Now →

How to Get Last Element of Array in PHP?

Read Now →

How to Get First Element of 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 →