How to Remove String Keys in PHP Array?
Hey Dev,
In this short tutorial, we will cover a php array remove string keys. This article will give you a simple example of how to remove string keys in array php. This article will give you a simple example of how to remove string keys in php array. This post will give you a simple example of how to delete string keys from array in php.
we will use array_filter() function with is_numeric and ARRAY_FILTER_USE_KEY to remove string keys from php array. so, let's see the simple code of how to delete string 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_numeric($k); },
ARRAY_FILTER_USE_KEY
);
var_dump($newArray);
?>
Output:
array(3) {
[1]=> string(3) "One"
[3]=> string(5) "Three"
[5]=> string(4) "Five"
}
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to Remove String Values in PHP Array?
- How to Remove Numeric Values in PHP Array?
- How to Get First 10 Elements of Array in PHP?
- How to Get First 5 Elements of Array in PHP?
- How to Get First 2 Elements of Array in PHP?
- How to Get Last 10 Elements of Array in PHP?
- Laravel Array Length Validation Example
- How to Get Maximum Key Value of Array in PHP?
- How to Remove Empty Values from Array in PHP?
- How to Add Prefix in Each Key of PHP Array?
- How to Get Minimum Key Value of Array in PHP?
- How to Remove Null Values from Array in PHP?