Use array_diff_key to remove

$remove = ['telephone', 'country'];

$remaining = array_diff_key($post, array_flip($remove));

You could use array_intersect_key if you wanted to supply an array of keys to keep.

Answer from Tim on Stack Overflow
๐ŸŒ
PHP
php.net โ€บ manual โ€บ en โ€บ function.unset.php
PHP: unset - Manual
A sample how to unset array elements from an array result coming from a mysql request. In this sample it is checking if a file exists and removes the row from the array if it not exists. <?php $db->set_query("select * from documents where document_in_user = 0"); //1 $documents = $db->result_to_array($db->get_result()); //1 foreach ($documents as $key => $row) { //2 $file = "uploads/".rawurldecode($row['document_name']); if ( file_exists ( $file ) == FALSE ) { unset($documents[$key]); //3 } } $documents = array_values($documents); // reindex the array (4) ?> variables: mysql table = documents, array = $documents array key (index) = $key array row (record sort of speak) = $row explanation: 1.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ php โ€บ how-to-remove-multiple-elements-from-an-array-in-php
How to Remove Multiple Elements from an Array in PHP? - GeeksforGeeks
July 31, 2024 - The foreach loop iterates over $remove, and for each element, array_search($value, $arr) finds the key, and unset($arr[$key]) removes the element. array_values($arr) reindexes the array to have sequential keys.
๐ŸŒ
Purpleturtlecreative
purpleturtlecreative.com โ€บ home โ€บ blog โ€บ how to recursively unset array keys and object properties in php
How to Recursively Unset Array Keys and Object Properties in PHP
September 10, 2024 - */ function deep_unset( array|object &$data, string $field ) { if ( is_array( $data ) ) { unset( $data[ $field ] ); } elseif ( is_object( $data ) ) { unset( $data->{$field} ); } foreach ( $data as &$value ) { if ( is_array( $value ) || is_object( $value ) ) { deep_unset( $value, $field ); } } }Code language: PHP (php) deep_unset( $employees, 'id' );// Remove all "id" properties and keys. print_r( $employees );// See the result.Code language: PHP (php) Additionally, you may want to recursively unset multiple fields at once.
๐ŸŒ
Plus2Net
plus2net.com โ€บ php_tutorial โ€บ array_unset.php
Deleting key & value element of array by using unset command in PHP
February 5, 2000 - Here is the code , we want to remove d from our $input array. $new_array=array_diff($input,array("d")); We can also use like this to remove more elements by using its value. $remove=array(c,f); $new_array=array_diff($input,$remove); Display & check our elements after removal like this. while (list ($key, $val) = each ($new_array)) { echo "$key -> $val <br>";} The output of the above command is here ยท 0 -> a 1 -> b 3 -> d 4 -> e 6 -> g You can read how unset a variable here.
๐ŸŒ
W3Schools
w3schools.com โ€บ php โ€บ php_arrays_remove.asp
PHP Remove Array Items
The unset() function takes a unlimited number of arguments, and can therefore be used to delete multiple array items: ... $cars = array("Volvo", "BMW", "Toyota"); unset($cars[0], $cars[1]); var_dump($cars); Try it Yourself ยป ยท To remove items ...
Find elsewhere
๐ŸŒ
Learn Code Web
learncodeweb.com โ€บ home โ€บ delete an array element based on a key in php?
Delete an array element based on a key in PHP? - Learn Code Web
January 25, 2021 - $inputNew = removeRecursive($input,'key1,key2,key3'); The above function is used to remove multiple array elements based on multiple keys. To check just copy and paste below example code in your PHP file and run the file.
๐ŸŒ
Tutsplanet
tutsplanet.com โ€บ home โ€บ how to remove multiple elements by key in php array?
How to Remove Multiple Elements by Key in PHP Array? - Tutsplanet
February 14, 2021 - $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: ... May be another day? Shop on Amazon using our links. Your prices won't change but we get a small commission. ... Upload Multiple Images and Store in Database using PHP and MySQL.
๐ŸŒ
Uptimia
uptimia.com โ€บ home โ€บ questions โ€บ how to delete an element from an array in php?
How to Delete an Element from an Array in PHP?
May 20, 2024 - Pass multiple keys as separate arguments to the unset() function to delete multiple elements in a single statement. Here's an example that demonstrates removing multiple elements from an array using unset():
๐ŸŒ
Koenwoortman
koenwoortman.com โ€บ php-remove-keys-from-associative-array
Remove key and value from an associative array in PHP
See the example below where two keys are dropped from the associative array. &LT?php $mascots = [ 'ElePHPant' => 'php', 'Geeko' => 'openSUSE', 'Gopher' => 'Go' ]; unset($mascots['Gopher'], $mascots['Geeko']); print_r($mascots); // Array // ( // [ElePHPant] => php // ) However useful, the approach above might get somewhat tedious when you need to remove multiple keys from the associative array.
๐ŸŒ
Morioh
morioh.com โ€บ p โ€บ 7efe62bdaedb
How to remove multiple keys from PHP Array?
morioh.com is your first and best source for all of the information youโ€™re looking for. From general topics to more of what you would expect to find here, morioh.com has it all. We hope you find what you are searching for!
Top answer
1 of 16
3648

There are different ways to delete an array element, where some are more useful for some specific tasks than others.

Deleting a Single Array Element

If you want to delete just one single array element you can use unset() and alternatively array_splice().

By key or by value?

If you know the value and don't know the key to delete the element you can use array_search() to get the key. This only works if the element doesn't occur more than once, since array_search() returns the first hit only.

unset() Expression

Note: When you use unset() the array keys wonโ€™t change. If you want to reindex the keys you can use array_values() after unset(), which will convert all keys to numerically enumerated keys starting from 0 (the array remains a list).

Example Code:

$array = [0 => "a", 1 => "b", 2 => "c"];
unset($array[1]);
          // โ†‘ Key of element to delete

Example Output:

[
    [0] => a
    [2] => c
]

array_splice() Function

If you use array_splice() the (integer) keys will automatically be reindex-ed, but the associative (string) keys won't change โ€” as opposed to array_values() after unset(), which will convert all keys to numerical keys.

Note: array_splice() needs the offset, not the key, as the second parameter; offset = array_flip(array_keys(array))[key].

Example Code:

$array = [0 => "a", 1 => "b", 2 => "c"];
array_splice($array, 1, 1);
                  // โ†‘ Offset of element to delete

Example Output:

[
    [0] => a
    [1] => c
]

array_splice(), same as unset(), take the array by reference. You donโ€™t assign the return values back to the array.

Deleting Multiple Array Elements

If you want to delete multiple array elements and donโ€™t want to call unset() or array_splice() multiple times you can use the functions array_diff() or array_diff_key() depending on whether you know the values or the keys of the elements to remove from the array.

array_diff() Function

If you know the values of the array elements which you want to delete, then you can use array_diff(). As before with unset() it wonโ€™t change the keys of the array.

Example Code:

$array = [0 => "a", 1 => "b", 2 => "c", 3 => "c"];
$array = array_diff($array, ["a", "c"]);
                         // โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                         // Array values to delete

Example Output:

[
    [1] => b
]

array_diff_key() Function

If you know the keys of the elements which you want to delete, then you want to use array_diff_key(). You have to make sure you pass the keys as keys in the second parameter and not as values. Keys wonโ€™t reindex.

Example Code:

$array = [0 => "a", 1 => "b", 2 => "c"];
$array = array_diff_key($array, [0 => "xy", "2" => "xy"]);
                              // โ†‘           โ†‘
                              // Array keys of elements to delete

Example Output:

[
    [1] => b
]

If you want to use unset() or array_splice() to delete multiple elements with the same value you can use array_keys() to get all the keys for a specific value and then delete all elements.

array_filter() Function

If you want to delete all elements with a specific value in the array you can use array_filter().

Example Code:

$array = [0 => "a", 1 => "b", 2 => "c"];
$array = array_filter($array, static function ($element) {
    return $element !== "b";
    //                   โ†‘
    // Array value which you want to delete
});

Example Output:

[
    [0] => a
    [2] => c
]
2 of 16
1429

It should be noted that unset() will keep indexes untouched, which is what you'd expect when using string indexes (array as hashtable), but can be quite surprising when dealing with integer indexed arrays:

$array = array(0, 1, 2, 3);
unset($array[2]);
var_dump($array);
/* array(3) {
  [0]=>
  int(0)
  [1]=>
  int(1)
  [3]=>
  int(3)
} */

$array = array(0, 1, 2, 3);
array_splice($array, 2, 1);
var_dump($array);
/* array(3) {
  [0]=>
  int(0)
  [1]=>
  int(1)
  [2]=>
  int(3)
} */

So array_splice() can be used if you'd like to normalize your integer keys. Another option is using array_values() after unset():

$array = array(0, 1, 2, 3);

unset($array[2]);
$array = array_values($array);
var_dump($array);
/* array(3) {
  [0]=>
  int(0)
  [1]=>
  int(1)
  [2]=>
  int(3)
} */
๐ŸŒ
YouTube
youtube.com โ€บ hey delphi
Array : how do i unset multiple keys in php - YouTube
Array : how do i unset multiple keys in phpTo Access My Live Chat Page, On Google, Search for "hows tech developer connect"As promised, I have a secret featu...
Published ย  April 13, 2023
Views ย  2
๐ŸŒ
CopyProgramming
copyprogramming.com โ€บ howto โ€บ php-php-array-unset-list-of-keys
Php: Removing multiple keys from a PHP array
May 24, 2023 - PHP array - remove elements with keys that start with, $arrinput = array_filter( $arrinput, function($key){ return strpos($key, 'not__') !== 0; }, ARRAY_FILTER_USE_KEY );. The flag parameters weren't ... Experimenting with my code based on the techniques shared in How to retrieve and modify ...
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ php unset() function
PHP unset() Function - Scaler Topics
September 29, 2023 - Simply separate each variable with a comma. $array ame: Refers to the name of the array in which you want to remove an element. key: Specifies the index/key of the element you wish to unset within the array.
๐ŸŒ
Tutorialio
tutorialio.com โ€บ delete-an-element-from-an-array-in-php
How can I Delete an Element from an Array in PHP?
May 20, 2022 - In this case, you will have to ... particular element. If you want to remove multiple consecutive elements from an array at once, you can use the array_splice() function....