• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
Tutsplanet

Tutsplanet

Free Technical and Blogging Resources

  • Home
  • Web Hosting
  • Programming
  • Plugins
  • Twitter Trends
  • Tools
  • About Us

Programming TagsPHP

How to Remove Multiple Elements by Key in PHP Array?

Feb 11, 2021 Editorial Staff

Share
Tweet
Share
1 Shares

Today’s article, we will remove multiple elements by key in a PHP array. Using PHP we can delete multiple keys from an Array.

In PHP there is no built in method to achieve this, but we can combine several PHP functions to achieve this.

In this article we will be demonstrating two methods to remove multiple keys from PHP array.

Method 1 : unset method

Below code we are removing two keys (hobby, like) from the array using unset method

$customer = [
       'name'=>'John Doe', 
       'email'=>'[email protected]', 
       'street'=>'Street 1, Avenue Drive', 
       'city'=>'Texas',
       'hobby' => 'Reading',
       'country' => 'US',
       'like' => 'Fishing'
  ];
$toRemove = ['hobby', 'like']; 

foreach($toRemove as $key){
    unset($customer[$key]);
}

print_r($customer);

Method 2 Using combination of array_diff_key and array_flip

$removeFields = ['hobby', 'like'];
$newCustomer = array_diff_key($customer, array_flip($removeFields));

print_r($newCustomer);
Did this post help you?
Tutsplanet brings in-depth and easy tutorials to understand even for beginners. This takes a considerable amount of work. If this post helps you, please consider supporting us as a token of appreciation:
  • Just want to thank us? Buy us a Coffee
  • May be another day? Shop on Amazon using our links.
    Your prices won't change but we get a small commission.

Editorial Staff

Editorial Staff at Tutsplanet is a dedicated team to write various tutorials about subjects like Programming, Technology and Operating Systems.

View all posts by Editorial Staff

Primary Sidebar

Quick Links

  • Top 21 Website Ideas To Make Money Online in 2021
  • A Simple YouTube Video Downloader Script in PHP
  • The 50 Most Useful jQuery Plugins for Frontend Development
  • Replace “\n” with new line characters, using Notepad++
  • Using Third-Party Libraries in Codeigniter
  • Upload Multiple Images and Store in Database using PHP and MySQL.
  • Hierarchical Tree view Category Example in Laravel
  • Laravel Image Intervention Tutorial With Example
  • How to import sql file in MySQL database using PHP?
  • Free VAT Calculator Online

Subscribe

* indicates required

Search Here

Share

   

Hot topics

  • Replace “\n” with new line characters, using Notepad++ 19 views
  • Add FTP/SFTP in Visual Studio Code 18 views
  • A Simple YouTube Video Downloader Script in PHP 17 views
  • Open a URL in a new tab using JavaScript 15 views
  • How to enter new line in Microsoft Teams? 13 views
  • Auto Login in PuTTY with a password 12 views
  • PHP: Implode () with quotes 10 views
  • Hierarchical Tree view Category Example in Laravel 10 views
  • Simple PHP Shopping Cart 9 views
  • All Countries SQL file with Country Codes to Download 9 views

Categories

  • Design & Development
  • Drupal
  • Facebook
  • General
  • How To
  • ios
  • Javascript
  • Linux
  • Magento
  • Marketing
  • News
  • PHP
  • Plugins
  • Programming
  • Snippets List
  • Social Media
  • Softwares
  • Themes
  • Tips
  • Wordpress
  • YouTube

Copyright © 2023 · Planet on Genesis Framework · Powered By BunnyCDN . Network wallpapernoon.com