Remove some keys from array php

I have an associative array of data and I have an array of keys I would like to remove from that array (while keeping the remaining keys in original order -- not that this is likely to be a constraint).

I am looking for a one liner of php to do this.
I already know how I could loop through the arrays but it seems there should be some array_map with unset or array_filter solution just outside of my grasp.

I have searched around for a bit but found nothing too concise.

To be clear this is the problem to do in one line:

//have this example associative array of data
$data = array(
    'blue'   => 43,
    'red'    => 87,
    'purple' => 130,
    'green'  => 12,
    'yellow' => 31
);

//and this array of keys to remove
$bad_keys = array(
    'purple',
    'yellow'
);

//some one liner here and then $data will only have the keys blue, red, green

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Given an array (One dimensional or multidimensional) and the task is to delete an array element based on key value.

    Examples:

    Input: Array
           (   
               [0] => 'G' 
               [1] => 'E'
               [2] => 'E'
               [3] => 'K'
               [4] => 'S'
           )
           Key = 2
    Output: Array
            (   
                [0] => 'G' 
                [1] => 'E'
                [3] => 'K'
                [4] => 'S'
            )
    

    Using unset() Function: The unset() function is used to remove element from the array. The unset function is used to destroy any other variable and same way use to delete any element of an array. This unset command takes the array key as input and removed that element from the array. After removal the associated key and value does not change.

    Syntax:

    unset($variable)

    Parameter: This function accepts single parameter variable. It is required parameter and used to unset the element.

    Program 1: Delete an element from one dimensional array.

    <?php

    $arr = array('G', 'E', 'E', 'K', 'S');

    print_r($arr);

    unset($arr[2]);

    print_r($arr);

    ?>

    Output:

    Array
    (
        [0] => G
        [1] => E
        [2] => E
        [3] => K
        [4] => S
    )
    Array
    (
        [0] => G
        [1] => E
        [3] => K
        [4] => S
    )
    

    Program 2: Delete an element from associative array.

    <?php 

    $marks = array

        "Ankit" => array

            "C" => 95, 

            "DCO" => 85, 

        ), 

        "Ram" => array

            "C" => 78, 

            "DCO" => 98, 

        ), 

        "Anoop" => array

            "C" => 88, 

            "DCO" => 46, 

        ), 

    ); 

    echo "Before delete the element <br>";

    print_r($marks); 

    unset($marks["Ram"]);

    echo "After delete the element <br>";

    print_r($marks); 

    ?> 

    Output:

    Before delete the element 
    Array ( [Ankit] => Array ( [C] => 95 [DCO] => 85 ) [Ram] => Array ( [C] => 78 [DCO] => 98 ) [Anoop] => Array ( [C] => 88 [DCO] => 46 ) ) After delete the element
    Array ( [Ankit] => Array ( [C] => 95 [DCO] => 85 ) [Anoop] => Array ( [C] => 88 [DCO] => 46 ) )

    PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.


    How do I remove a specific key from an array?

    Using unset() Function: The unset() function is used to remove element from the array. The unset function is used to destroy any other variable and same way use to delete any element of an array. This unset command takes the array key as input and removed that element from the array.

    How do you unset multiple values in an array in PHP?

    $name = $post['name']; $email = $post['email']; $address = $post['address']; $telephone = $post['telephone']; $country = $post['country']; unset($post['name']); unset($post['email']); unset($post['address']); unset($post['telephone']); unset($post['country']);

    How do you delete an element from an array?

    Array elements can be deleted using the JavaScript operator delete . Using delete leaves undefined holes in the array. Use pop() or shift() instead.

    What does Array_splice () function do give an example?

    The array_splice() function removes selected elements from an array and replaces it with new elements. The function also returns an array with the removed elements. Tip: If the function does not remove any elements (length=0), the replaced array will be inserted from the position of the start parameter (See Example 2).