Hướng dẫn how to delete an array element based on value in php? - làm thế nào để xóa một phần tử mảng dựa trên giá trị trong php?

Đã mượn logic của dấu gạch dưới.js _.reject và tạo hai chức năng (mọi người thích các chức năng !!)

Array_reject_Value: Hàm này chỉ đơn giản là từ chối giá trị được chỉ định (cũng hoạt động cho Php4,5,7) This function is simply rejecting the value specified (also works for PHP4,5,7)

function array_reject_value(array &$arrayToFilter, $deleteValue) {
    $filteredArray = array();

    foreach ($arrayToFilter as $key => $value) {
        if ($value !== $deleteValue) {
            $filteredArray[] = $value;
        }
    }

    return $filteredArray;
}

Array_reject: Hàm này chỉ đơn giản là từ chối phương thức có thể gọi được (hoạt động cho PHP> = 5.3) This function is simply rejecting the callable method (works for PHP >=5.3)

function array_reject(array &$arrayToFilter, callable $rejectCallback) {

    $filteredArray = array();

    foreach ($arrayToFilter as $key => $value) {
        if (!$rejectCallback($value, $key)) {
            $filteredArray[] = $value;
        }
    }

    return $filteredArray;
}

Vì vậy, trong ví dụ hiện tại của chúng tôi, chúng tôi có thể sử dụng các chức năng trên như sau:

$messages = [312, 401, 1599, 3, 6];
$messages = array_reject_value($messages, 401);

hoặc thậm chí tốt hơn: (vì điều này cung cấp cho chúng ta một cú pháp tốt hơn để sử dụng như Array_Filter One)

$messages = [312, 401, 1599, 3, 6];
$messages = array_reject($messages, function ($value) {
    return $value === 401;
});

Những thứ trên có thể được sử dụng cho những thứ phức tạp hơn như giả sử chúng tôi muốn xóa tất cả các giá trị lớn hơn hoặc bằng 401 mà chúng tôi có thể làm điều này:

$messages = [312, 401, 1599, 3, 6];
$greaterOrEqualThan = 401;
$messages = array_reject($messages, function ($value) use $greaterOrEqualThan {
    return $value >= $greaterOrEqualThan;
});

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc

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

    Bàn luận{IDE} first, before moving on to the solution.

    Cho một mảng (một chiều hoặc đa chiều) và nhiệm vụ là xóa một phần tử mảng dựa trên giá trị khóa. 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)

    Được đề xuất: Vui lòng thử cách tiếp cận của bạn trên {IDE} trước, trước khi chuyển sang giải pháp. This function accepts single parameter variable. It is required parameter and used to unset the element.

    Sử dụng hàm unSet (): hàm unset () được sử dụng để loại bỏ phần tử khỏi mảng. Hàm Untet được sử dụng để phá hủy bất kỳ biến nào khác và cùng cách sử dụng để xóa bất kỳ yếu tố nào của mảng. Lệnh Untet này lấy phím mảng làm đầu vào và loại bỏ phần tử đó khỏi mảng. Sau khi loại bỏ khóa và giá trị liên quan không thay đổi. Delete an element from one dimensional array.

    Tham số: Hàm này chấp nhận biến tham số đơn. Nó được yêu cầu tham số và được sử dụng để hủy bỏ phần tử.

    $messages = [312, 401, 1599, 3, 6];
    $messages = array_reject_value($messages, 401);
    
    4
    function array_reject(array &$arrayToFilter, callable $rejectCallback) {
    
        $filteredArray = array();
    
        foreach ($arrayToFilter as $key => $value) {
            if (!$rejectCallback($value, $key)) {
                $filteredArray[] = $value;
            }
        }
    
        return $filteredArray;
    }
    
    0
    $messages = [312, 401, 1599, 3, 6];
    $messages = array_reject_value($messages, 401);
    
    3

    $messages = [312, 401, 1599, 3, 6];
    $messages = array_reject_value($messages, 401);
    
    7
    function array_reject(array &$arrayToFilter, callable $rejectCallback) {
    
        $filteredArray = array();
    
        foreach ($arrayToFilter as $key => $value) {
            if (!$rejectCallback($value, $key)) {
                $filteredArray[] = $value;
            }
        }
    
        return $filteredArray;
    }
    
    0
    $messages = [312, 401, 1599, 3, 6];
    $messages = array_reject_value($messages, 401);
    
    9

    $messages = [312, 401, 1599, 3, 6];
    $messages = array_reject_value($messages, 401);
    
    4
    function array_reject(array &$arrayToFilter, callable $rejectCallback) {
    
        $filteredArray = array();
    
        foreach ($arrayToFilter as $key => $value) {
            if (!$rejectCallback($value, $key)) {
                $filteredArray[] = $value;
            }
        }
    
        return $filteredArray;
    }
    
    0
    $messages = [312, 401, 1599, 3, 6];
    $messages = array_reject_value($messages, 401);
    
    3

    $messages = [312, 401, 1599, 3, 6];
    $messages = array_reject($messages, function ($value) {
        return $value === 401;
    });
    
    3

    Output:

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

    Chương trình 1: Xóa một phần tử khỏi mảng một chiều. Delete an element from associative array.

    $messages = [312, 401, 1599, 3, 6];
    $messages = array_reject($messages, function ($value) {
        return $value === 401;
    });
    
    4

    Is

    Chương trình 2: Xóa một yếu tố từ mảng kết hợp.

    $messages = [312, 401, 1599, 3, 6];
    $messages = array_reject($messages, function ($value) {
        return $value === 401;
    });
    
    5
    function array_reject(array &$arrayToFilter, callable $rejectCallback) {
    
        $filteredArray = array();
    
        foreach ($arrayToFilter as $key => $value) {
            if (!$rejectCallback($value, $key)) {
                $filteredArray[] = $value;
            }
        }
    
        return $filteredArray;
    }
    
    1
    function array_reject(array &$arrayToFilter, callable $rejectCallback) {
    
        $filteredArray = array();
    
        foreach ($arrayToFilter as $key => $value) {
            if (!$rejectCallback($value, $key)) {
                $filteredArray[] = $value;
            }
        }
    
        return $filteredArray;
    }
    
    2
    $messages = [312, 401, 1599, 3, 6];
    $messages = array_reject($messages, function ($value) {
        return $value === 401;
    });
    
    8

    $messages = [312, 401, 1599, 3, 6];
    $messages = array_reject($messages, function ($value) {
        return $value === 401;
    });
    
    9
    $messages = [312, 401, 1599, 3, 6];
    $greaterOrEqualThan = 401;
    $messages = array_reject($messages, function ($value) use $greaterOrEqualThan {
        return $value >= $greaterOrEqualThan;
    });
    
    0
    $messages = [312, 401, 1599, 3, 6];
    $greaterOrEqualThan = 401;
    $messages = array_reject($messages, function ($value) use $greaterOrEqualThan {
        return $value >= $greaterOrEqualThan;
    });
    
    1
    function array_reject(array &$arrayToFilter, callable $rejectCallback) {
    
        $filteredArray = array();
    
        foreach ($arrayToFilter as $key => $value) {
            if (!$rejectCallback($value, $key)) {
                $filteredArray[] = $value;
            }
        }
    
        return $filteredArray;
    }
    
    2
    $messages = [312, 401, 1599, 3, 6];
    $messages = array_reject($messages, function ($value) {
        return $value === 401;
    });
    
    8

    $messages = [312, 401, 1599, 3, 6];
    $messages = array_reject($messages, function ($value) {
        return $value === 401;
    });
    
    9
    Input: Array
           (   
               [0] => 'G' 
               [1] => 'E'
               [2] => 'E'
               [3] => 'K'
               [4] => 'S'
           )
           Key = 2
    Output: Array
            (   
                [0] => 'G' 
                [1] => 'E'
                [3] => 'K'
                [4] => 'S'
            )
    
    1

    $messages = [312, 401, 1599, 3, 6];
    $greaterOrEqualThan = 401;
    $messages = array_reject($messages, function ($value) use $greaterOrEqualThan {
        return $value >= $greaterOrEqualThan;
    });
    
    4
    $messages = [312, 401, 1599, 3, 6];
    $greaterOrEqualThan = 401;
    $messages = array_reject($messages, function ($value) use $greaterOrEqualThan {
        return $value >= $greaterOrEqualThan;
    });
    
    5
    $messages = [312, 401, 1599, 3, 6];
    $greaterOrEqualThan = 401;
    $messages = array_reject($messages, function ($value) use $greaterOrEqualThan {
        return $value >= $greaterOrEqualThan;
    });
    
    6

    $messages = [312, 401, 1599, 3, 6];
    $greaterOrEqualThan = 401;
    $messages = array_reject($messages, function ($value) use $greaterOrEqualThan {
        return $value >= $greaterOrEqualThan;
    });
    
    4
    $messages = [312, 401, 1599, 3, 6];
    $greaterOrEqualThan = 401;
    $messages = array_reject($messages, function ($value) use $greaterOrEqualThan {
        return $value >= $greaterOrEqualThan;
    });
    
    8
    $messages = [312, 401, 1599, 3, 6];
    $greaterOrEqualThan = 401;
    $messages = array_reject($messages, function ($value) use $greaterOrEqualThan {
        return $value >= $greaterOrEqualThan;
    });
    
    9

    $messages = [312, 401, 1599, 3, 6];
    $messages = array_reject($messages, function ($value) {
        return $value === 401;
    });
    
    9
    Input: Array
           (   
               [0] => 'G' 
               [1] => 'E'
               [2] => 'E'
               [3] => 'K'
               [4] => 'S'
           )
           Key = 2
    Output: Array
            (   
                [0] => 'G' 
                [1] => 'E'
                [3] => 'K'
                [4] => 'S'
            )
    
    3
    $messages = [312, 401, 1599, 3, 6];
    $greaterOrEqualThan = 401;
    $messages = array_reject($messages, function ($value) use $greaterOrEqualThan {
        return $value >= $greaterOrEqualThan;
    });
    
    1
    function array_reject(array &$arrayToFilter, callable $rejectCallback) {
    
        $filteredArray = array();
    
        foreach ($arrayToFilter as $key => $value) {
            if (!$rejectCallback($value, $key)) {
                $filteredArray[] = $value;
            }
        }
    
        return $filteredArray;
    }
    
    2
    $messages = [312, 401, 1599, 3, 6];
    $messages = array_reject($messages, function ($value) {
        return $value === 401;
    });
    
    8

    $messages = [312, 401, 1599, 3, 6];
    $messages = array_reject($messages, function ($value) {
        return $value === 401;
    });
    
    9
    Input: Array
           (   
               [0] => 'G' 
               [1] => 'E'
               [2] => 'E'
               [3] => 'K'
               [4] => 'S'
           )
           Key = 2
    Output: Array
            (   
                [0] => 'G' 
                [1] => 'E'
                [3] => 'K'
                [4] => 'S'
            )
    
    1

    $messages = [312, 401, 1599, 3, 6];
    $greaterOrEqualThan = 401;
    $messages = array_reject($messages, function ($value) use $greaterOrEqualThan {
        return $value >= $greaterOrEqualThan;
    });
    
    4
    $messages = [312, 401, 1599, 3, 6];
    $greaterOrEqualThan = 401;
    $messages = array_reject($messages, function ($value) use $greaterOrEqualThan {
        return $value >= $greaterOrEqualThan;
    });
    
    5
    Input: Array
           (   
               [0] => 'G' 
               [1] => 'E'
               [2] => 'E'
               [3] => 'K'
               [4] => 'S'
           )
           Key = 2
    Output: Array
            (   
                [0] => 'G' 
                [1] => 'E'
                [3] => 'K'
                [4] => 'S'
            )
    
    9

    $messages = [312, 401, 1599, 3, 6];
    $greaterOrEqualThan = 401;
    $messages = array_reject($messages, function ($value) use $greaterOrEqualThan {
        return $value >= $greaterOrEqualThan;
    });
    
    4
    $messages = [312, 401, 1599, 3, 6];
    $greaterOrEqualThan = 401;
    $messages = array_reject($messages, function ($value) use $greaterOrEqualThan {
        return $value >= $greaterOrEqualThan;
    });
    
    8
    unset($variable)
    2

    $messages = [312, 401, 1599, 3, 6];
    $messages = array_reject($messages, function ($value) {
        return $value === 401;
    });
    
    9
    unset($variable)
    6
    $messages = [312, 401, 1599, 3, 6];
    $greaterOrEqualThan = 401;
    $messages = array_reject($messages, function ($value) use $greaterOrEqualThan {
        return $value >= $greaterOrEqualThan;
    });
    
    1
    function array_reject(array &$arrayToFilter, callable $rejectCallback) {
    
        $filteredArray = array();
    
        foreach ($arrayToFilter as $key => $value) {
            if (!$rejectCallback($value, $key)) {
                $filteredArray[] = $value;
            }
        }
    
        return $filteredArray;
    }
    
    2
    $messages = [312, 401, 1599, 3, 6];
    $messages = array_reject($messages, function ($value) {
        return $value === 401;
    });
    
    8

    $messages = [312, 401, 1599, 3, 6];
    $messages = array_reject($messages, function ($value) {
        return $value === 401;
    });
    
    9
    Input: Array
           (   
               [0] => 'G' 
               [1] => 'E'
               [2] => 'E'
               [3] => 'K'
               [4] => 'S'
           )
           Key = 2
    Output: Array
            (   
                [0] => 'G' 
                [1] => 'E'
                [3] => 'K'
                [4] => 'S'
            )
    
    1

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

    $messages = [312, 401, 1599, 3, 6];
    $greaterOrEqualThan = 401;
    $messages = array_reject($messages, function ($value) use $greaterOrEqualThan {
        return $value >= $greaterOrEqualThan;
    });
    
    4
    $messages = [312, 401, 1599, 3, 6];
    $greaterOrEqualThan = 401;
    $messages = array_reject($messages, function ($value) use $greaterOrEqualThan {
        return $value >= $greaterOrEqualThan;
    });
    
    5
    Array
    (
        [0] => G
        [1] => E
        [2] => E
        [3] => K
        [4] => S
    )
    Array
    (
        [0] => G
        [1] => E
        [3] => K
        [4] => S
    )
    
    2

    $messages = [312, 401, 1599, 3, 6];
    $messages = array_reject_value($messages, 401);
    
    4
    $messages = [312, 401, 1599, 3, 6];
    $messages = array_reject($messages, function ($value) {
        return $value === 401;
    });
    
    5
    Array
    (
        [0] => G
        [1] => E
        [2] => E
        [3] => K
        [4] => S
    )
    Array
    (
        [0] => G
        [1] => E
        [3] => K
        [4] => S
    )
    
    8

    $messages = [312, 401, 1599, 3, 6];
    $messages = array_reject_value($messages, 401);
    
    7
    $messages = [312, 401, 1599, 3, 6];
    $messages = array_reject($messages, function ($value) {
        return $value === 401;
    });
    
    5
    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 ) )
    7
    Input: Array
           (   
               [0] => 'G' 
               [1] => 'E'
               [2] => 'E'
               [3] => 'K'
               [4] => 'S'
           )
           Key = 2
    Output: Array
            (   
                [0] => 'G' 
                [1] => 'E'
                [3] => 'K'
                [4] => 'S'
            )
    
    3
    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 ) )
    9

    $messages = [312, 401, 1599, 3, 6];
    $greaterOrEqualThan = 401;
    $messages = array_reject($messages, function ($value) use $greaterOrEqualThan {
        return $value >= $greaterOrEqualThan;
    });
    
    4
    $messages = [312, 401, 1599, 3, 6];
    $greaterOrEqualThan = 401;
    $messages = array_reject($messages, function ($value) use $greaterOrEqualThan {
        return $value >= $greaterOrEqualThan;
    });
    
    8
    Array
    (
        [0] => G
        [1] => E
        [2] => E
        [3] => K
        [4] => S
    )
    Array
    (
        [0] => G
        [1] => E
        [3] => K
        [4] => S
    )
    
    5

    Array
    (
        [0] => G
        [1] => E
        [2] => E
        [3] => K
        [4] => S
    )
    Array
    (
        [0] => G
        [1] => E
        [3] => K
        [4] => S
    )
    
    9
    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 ) )
    0
    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 ) )
    1

    6

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

    Array
    (
        [0] => G
        [1] => E
        [2] => E
        [3] => K
        [4] => S
    )
    Array
    (
        [0] => G
        [1] => E
        [3] => K
        [4] => S
    )
    
    9 1
    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 ) )
    1


    Làm cách nào để loại bỏ một mục cụ thể khỏi một mảng PHP?

    Để loại bỏ một phần tử khỏi một mảng, chúng ta có thể sử dụng hàm unset () loại bỏ phần tử khỏi mảng và sau đó sử dụng hàm Array_Values ​​() để lập chỉ mục cho mảng tự động.use unset() function which removes the element from an array and then use array_values() function which indexes the array numerically automatically.

    Làm thế nào để bạn loại bỏ một phần tử khỏi một mảng có giá trị?

    Sử dụng splice () để loại bỏ mục tùy ý cách chính xác để loại bỏ một mục khỏi mảng là sử dụng splice (). Nó cần một chỉ mục và số lượng các mục để xóa bắt đầu từ chỉ mục đó. Đừng nhầm lẫn điều này với lát cắt tương tự () () được sử dụng để trích xuất một phần của một mảng. The correct way to remove an item from an array is to use splice() . It takes an index and amount of items to delete starting from that index. Don't confuse this with its similar cousin slice() that is used to extract a section of an array.

    Array_Keys () được sử dụng trong PHP là gì?

    Array_Keys () là một hàm tích hợp trong PHP và được sử dụng để trả về tất cả các khóa và mảng hoặc tập hợp con của các khóa.Tham số: Hàm lấy ba tham số trong đó một tham số là bắt buộc và hai tham số khác là tùy chọn.to return either all the keys of and array or the subset of the keys. Parameters: The function takes three parameters out of which one is mandatory and other two are optional.

    Làm thế nào để bạn loại bỏ một mục khỏi một mảng?

    Nếu bạn muốn xóa một mục khỏi một mảng, bạn có thể sử dụng phương thức pop () để xóa phần tử cuối cùng hoặc phương thức Shift () để xóa phần tử đầu tiên.use the pop() method to remove the last element or the shift() method to remove the first element.