ItSolutionStuff.com

How to Remove Multiple Keys from PHP Array?

By Hardik Savani • May 14, 2024
PHP

In this example, we will remove array elements by keys array in php. we can delete multiple keys from array php. basically we will unset multiple keys from php array.

if you see in php documentation there are not available directly remove multiple keys from php array. But we will create our own php custom function and remove all keys by given array value.

In this example i created custom function as array_except(). you need to pass one main array and another will be keys array that you want to remove it.

So let's see bellow example:

Example:

<?php

$myArray = [

'name'=>'Hardik Savani',

'email'=>'savanihd@gmail.com',

'gender'=>'male',

'website'=>'itsolutionstuff.com'

];

$newArray = array_except($myArray, ['gender', 'email']);

print_r($newArray);

function array_except($array, $keys){

foreach($keys as $key){

unset($array[$key]);

}

return $array;

}

?>

Output:

Array

(

[name] => Hardik Savani

[website] => itsolutionstuff.com

)

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

PHP MySQL Create Dynamic Treeview Example

Read Now →

PHP MySQL Column Sorting Example Tutorial

Read Now →

PHP MySQL Login with Google Account Example

Read Now →

PHP MySQL DataTables Server-side Processing Example

Read Now →

How to Get File Name without Extension in PHP?

Read Now →

How to Remove White Space from String in PHP?

Read Now →

How to Remove Specific Element by Value from Array in PHP?

Read Now →

How to Remove undefined Value from Array in JQuery?

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 →

How can Make an Array from the Values of Another Array's Key Value?

Read Now →