Tutorial Details

How to Remove Numeric Keys in PHP Array
25 Nov

How to Remove Numeric Keys in PHP Array

Hi Guys,

I am going to show you an example of a php array removing numeric keys. you can understand the concept of how to remove numeric keys in array php. This example will help you how to remove numeric keys in a php array. I explained simply step by step how to delete number keys from an array in PHP. follow the below example for how to delete numeric keys from an 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 the 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...

0 Comments

Leave a Comment