Php drop array keys

PHP Array Remove Keys and Keep Values Example

In this small post, i want to show you how to remove keys and keep values in php array basically how to reindex array start from 0 index in php. we can make it done using array_values function of php array.

array_values() will re-create array with new keys so basically array_values function will create new array with new key like default 0 1 2 etc. if you want to delete keys and keep values with new array then in php.

See bellow simple example with output so, it will help you to make better sanes.

$myArray = [

‘paresh’ => ‘Paresh’,

‘hardik’ => ‘Hardik’,

‘vimal’ => ‘Vimal’,

‘harshad’ => ‘Harshad’

];

$reCreateArray = array_values($myArray);

print_r($reCreateArray);

Array

(

[0] => Paresh [1] => Hardik [2] => Vimal [3] => Harshad

)

Hardik Savani

I’m a full-stack developer, entrepreneur and owner of Aatman Infotech. 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

  • PHP Dropzone Display Existing Files from Server Example
  • PHP Ajax Dependent Dropdown List Example
  • How to get Keys from Array in PHP Laravel?
  • How to Set Value as Key in PHP Array?
  • How to Convert Object into Array in PHP?
  • How to Remove Specific Value from Array in JQuery?
  • How to Count Number of Files in Directory using PHP?
  • How to Remove Specific Element by Value from Array in PHP?
  • How to Remove undefined Value from Array in JQuery?
  • 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?
Читайте также:  Php soap request with xml
  • Codeigniter Restful API Tutorial Example
  • PHP Bootstrap Autocomplete Tokenfield using Ajax Example
  • PHP JQuery Ajax Post Request Example
  • Laravel 5.6 — User Roles and Permissions (ACL) using Spatie Tutorial
  • PHP AngularJS Populate Dynamic Dropdown Example
  • Codeigniter 3 and AngularJS CRUD with Search and Pagination Example.
  • Laravel Change Date Format using Carbon Example
  • PHP Ajax Multiple Image Upload with Preview Example
  • Laravel Join with Subquery in Query Builder Example
  • PHP Behance API Tutorial with Example
  • Laravel Block/Whitelist IP Address Tutorial

Источник

Removing Keys from an Array in PHP

Removing one or more keys from an array in PHP seems like something that should be easy to do. Yet out of the 70+ array functions in PHP, there is no single function to do it. But here’s the easy way:

Use array_diff_key()

PHP has a function called array_diff_key($array1, $array2) that returns an array with all of the keys (and their values) from $array1 that don’t also occur in $array2. We can use this to quickly cobble together a solution:

 function array_remove_keys($array, $keys) { // array_diff_key() expected an associative array. $assocKeys = array(); foreach($keys as $key) { $assocKeys[$key] = true; } return array_diff_key($array, $assocKeys); } // Example: $data = array( 'name' => 'Brian', 'address1' => '98 Market St.', 'address2' => 'N/A' ); // Output before array_remove_keys() var_dump($data); // Remove address2 key. $data = array_remove_keys($data, array('address2')); // Output after array_remove_keys() var_dump($data); /* Output: array(3) < ["name"]=>string(5) "Brian" ["address1"]=> string(13) "98 Market St." ["address2"]=> string(3) "N/A" > array(2) < ["name"]=>string(5) "Brian" ["address1"]=> string(13) "98 Market St." > */

return array_diff_key($array, $assocKeys); > // Example: $data = array( ‘name’ => ‘Brian’, ‘address1′ => ’98 Market St.’, ‘address2’ => ‘N/A’ ); // Output before array_remove_keys() var_dump($data); // Remove address2 key. $data = array_remove_keys($data, array(‘address2’)); // Output after array_remove_keys() var_dump($data); /* Output: array(3) < ["name"]=>string(5) «Brian» [«address1»]=> string(13) «98 Market St.» [«address2»]=> string(3) «N/A» > array(2) < ["name"]=>string(5) «Brian» [«address1»]=> string(13) «98 Market St.» > */

Works great! But it’s not quite complete. There is absolutely no error checking, and passing an array as the second parameter isn’t the most convenient way to operate. Let’s give the caller the option of passing either an array, or a comma-separated list of keys to delete.

 function array_remove_keys($array, $keys = array())  // If array is empty or not an array at all, don't bother // doing anything else. if(empty($array)  // If $keys is a comma-separated list, convert to an array. if(is_string($keys)) { $keys = explode(',', $keys); } // At this point if $keys is not an array, we can't do anything with it. if(! is_array($keys)) { return $array; } // array_diff_key() expected an associative array. $assocKeys = array(); foreach($keys as $key) { $assocKeys[$key] = true; } return array_diff_key($array, $assocKeys); } // Example: $data = array( 'name' => 'Brian', 'address1' => '98 Market St.', 'address2' => 'N/A' ); // Output before array_remove_keys() var_dump($data); // Remove address2 key. $data = array_remove_keys($data, 'address2'); // Output after array_remove_keys() var_dump($data); /* Output: array(3) < ["name"]=>string(5) "Brian" ["address1"]=> string(13) "98 Market St." ["address2"]=> string(3) "N/A" > array(2) < ["name"]=>string(5) "Brian" ["address1"]=> string(13) "98 Market St." > */

// If $keys is a comma-separated list, convert to an array. if(is_string($keys)) < $keys = explode(',', $keys); >// At this point if $keys is not an array, we can’t do anything with it. if(! is_array($keys)) < return $array; >// array_diff_key() expected an associative array. $assocKeys = array(); foreach($keys as $key) < $assocKeys[$key] = true; >return array_diff_key($array, $assocKeys); > // Example: $data = array( ‘name’ => ‘Brian’, ‘address1′ => ’98 Market St.’, ‘address2’ => ‘N/A’ ); // Output before array_remove_keys() var_dump($data); // Remove address2 key. $data = array_remove_keys($data, ‘address2’); // Output after array_remove_keys() var_dump($data); /* Output: array(3) < ["name"]=>string(5) «Brian» [«address1»]=> string(13) «98 Market St.» [«address2»]=> string(3) «N/A» > array(2) < ["name"]=>string(5) «Brian» [«address1»]=> string(13) «98 Market St.» > */

Share this entry

/wp-content/uploads/2019/09/reich-web-consulting-logo-o.svg 0 0 Brian Reich /wp-content/uploads/2019/09/reich-web-consulting-logo-o.svg Brian Reich 2010-12-15 14:03:52 2019-07-30 22:19:16 Removing Keys from an Array in PHP

Источник

Remove keys from an associative array in PHP

Today we’ll write a short article on how to remove keys from an associative array in PHP and get an array without keys to return a new reindex array which starts with index 0.

Ways to remove keys from associative array

1. Using built-in function

We have built-in array_values() function to remove the keys and re-create the array with new keys starting from 0. It returns an indexed array of values.

It required only a single parameter that specifies the array. Refer to the following example.

2. Using custom function

We can also remove the keys and get an array values using foreach loop with custom function.

That’s it for today.
Thank you for reading. Happy Coding.

You may also like.

Date range search with jQuery Datepicker using Ajax, PHP & MySQL - Clue Mediator

Date range search with jQuery Datepicker using Ajax, PHP & MySQL

Convert XML to JSON in PHP - Clue Mediator

Convert XML to JSON in PHP

Resize an image using the GD library in PHP - Clue Mediator

Resize an image using the GD library in PHP

Enable CORS for multiple domains in PHP - Clue Mediator

Enable CORS for multiple domains in PHP

Find URLs in a string and make clickable links in PHP - Clue Mediator

Find URLs in a string and make clickable links in PHP

How to convert Camel Case to Snake Case in PHP - Clue Mediator

How to convert Camel Case to Snake Case in PHP

Leave a Reply Cancel reply

Search your query

Recent Posts

  • Connect to a MySQL Database Using the MySQL Command: A Comprehensive Guide July 16, 2023
  • Connecting to SSH using a PEM File July 15, 2023
  • How to Add the Body to the Mailto Link July 14, 2023
  • How to Add a Subject Line to the Email Link July 13, 2023
  • How to Create Mail and Phone Links in HTML July 12, 2023

Tags

Join us

Top Posts

Explore the article

We are not simply proficient at writing blog post, we’re excellent at explaining the way of learning which response to developers.

For any inquiries, contact us at [email protected] .

  • We provide the best solution to your problem.
  • We give you an example of each article.
  • Provide an example source code for you to download.
  • We offer live demos where you can play with them.
  • Quick answers to your questions via email or comment.

Clue Mediator © 2023. All Rights Reserved.

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.

Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.

Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.

Источник

Оцените статью