Php remove keys from array

Although an array in PHP is "a type that associates values to keys" and some say it's not possible to remove the keys, you might, in a sense, remove the keys from an array either replacing the keys or replacing the array with another array.

I suppose you would like to change the array to an array equivalent to an array initialized without explicit keys. For instance, you might have this array:

$arr = array(2 => 'a', 3 => 'b', 4 => 'c');

But you'd like it to be equivalent to:

$arr = array('a', 'b', 'c');

It's possible to do it using the function "array_values", like this:

$arr = array_values($arr);

Notice, though, the array still has keys: 0, 1, 2, and 3.

It's important to note that the integer keys are not the same as the array indexes. For instance, this piece of code:

 '', 4 => '', 1 => '', 2 => '');
foreach($arr as $k => $v):
    echo "$k ";
endforeach;

will output:

3 4 1 2

The function "array_values" might be used to make sure the keys values and indexes are the same:

 '', 4 => '', 1 => '', 2 => '');
    foreach(array_values($arr) as $k => $v):
        echo "$k ";
    endforeach;
    // outputs: 0, 1, 2, 3

I explained how to "remove" the keys, but you might want to implement a class for the topics, make an interface for topic contents (animations, images, etc.) and check the SPL data structures.

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.

    $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.

    $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
    "
    ;

    print_r($marks); 

    unset($marks["Ram"]);

    echo "After delete the element
    "
    ;

    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 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 delete associative array in PHP?

    Answer: Use the PHP unset() Function If you want to delete an element from an array you can simply use the unset() function. The following example shows how to delete an element from an associative array and numeric array.

    Does PHP have flip function?

    PHP | array_flip() Function This built-in function of PHP is used to exchange elements within an array, i.e., exchange all keys with their associated values in an array and vice-versa. We must remember that the values of the array need to be valid keys, i.e. they need to be either integer or string.

    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).