Home  >  Article  >  Backend Development  >  How to delete array elements based on key in PHP

How to delete array elements based on key in PHP

藏色散人
藏色散人Original
2019-02-12 14:24:323319browse

Given an array (one or more dimensions), the task is to delete an array element based on the key value.

How to delete array elements based on key in PHP

The example is as follows:

输入: Array
       (   
           [0] => 'G' 
           [1] => 'E'
           [2] => 'E'
           [3] => 'K'
           [4] => 'S'
       )
       Key = 2
输出: Array
        (   
            [0] => 'G' 
            [1] => 'E'
            [3] => 'K'
            [4] => 'S'
        )

Use the unset() function: The unset() function is used to delete elements from an array. The unset function is used to destroy any other variable and is also used to delete any element of an array. This unset command takes an array key as input and removes the element from the array. After deletion, the associated keys and values ​​do not change.

Syntax:

unset($variable)

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

Procedure 1: Remove elements from a one-dimensional array.

<?php 
$arr = array(&#39;G&#39;, &#39;E&#39;, &#39;E&#39;, &#39;K&#39;, &#39;S&#39;);  
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: Remove elements from an associative array.

<?php   
$marks = array(     
    "Ankit" => array(                   
        "C" => 95,  
        "DCO" => 85,  
    ),            
   
    "Ram" => array(            
        "C" => 78,  
        "DCO" => 98,  
    ),  
       
    "Anoop" => array(            
        "C" => 88,  
        "DCO" => 46,  
    ),  
);    
echo "删除元素前 <br>";   
print_r($marks);  
 
unset($marks["Ram"]);   
echo "删除元素后 <br>";   

print_r($marks);  
  
?>

Output:

删除元素前 
Array
(
    [Ankit] => Array
        (
            [C] => 95
            [DCO] => 85
        )

    [Ram] => Array
        (
            [C] => 78
            [DCO] => 98
        )

    [Anoop] => Array
        (
            [C] => 88
            [DCO] => 46
        )

)
删除元素后
Array
(
    [Ankit] => Array
        (
            [C] => 95
            [DCO] => 85
        )

    [Anoop] => Array
        (
            [C] => 88
            [DCO] => 46
        )

)

Recommendation: "PHP Tutorial"

This article is about how to delete key-based keys in PHP The method introduction of array elements is simple and easy to understand. I hope it will be helpful to friends in need!

The above is the detailed content of How to delete array elements based on key in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn