How to delete array elements based on key in PHP

藏色散人
Release: 2023-04-05 07:36:01
Original
3335 people have browsed it

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'
        )
Copy after login

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)
Copy after login

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); 
  
?>
Copy after login

Output:

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

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);  
  
?>
Copy after login

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
        )

)
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template