How to remove multiple keys from PHP Array?

How to remove multiple keys from PHP Array?

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

)

About Author

Leave a Reply

Your email address will not be published. Required fields are marked *

PAGE TOP
error

Enjoy this blog? Please spread the word :)

RSS
Follow by Email