Remove Specific Element from an Array in PHP Last Updated : 23 May, 2024 Comments Improve Suggest changes Like Article Like Report Given an Array containing some elements, the task is to remove specific elements from the array in PHP. Below are the approaches to remove specific elements from an array in PHP: Table of Content Using unset() FunctionUsing array_diff() FunctionUsing array_filter() FunctionUsing array_splice() FunctionUsing foreach LoopUsing unset() FunctionThe unset() function is used to remove an element from an array by its key. This method works well when you know the key of the element to be removed. Example: The unset() function removes the element at index 1 ("CSS") from the array. PHP <?php $arr = ["HTML", "CSS", "JavaScript", "PHP"]; $key = 1; unset($arr[$key]); print_r($arr); ?> OutputArray ( [0] => HTML [2] => JavaScript [3] => PHP ) Using array_diff() FunctionThe array_diff() function compares arrays and returns the values in the first array that are not present in any of the other arrays. This can be used to remove specific elements by value. Example: The array_diff() function removes the element "CSS" from the array. PHP <?php $arr = ["HTML", "CSS", "JavaScript", "PHP"]; $remVal = "CSS"; $arr = array_diff($arr, [$remVal]); print_r($arr); ?> OutputArray ( [0] => HTML [2] => JavaScript [3] => PHP ) Using array_filter() FunctionThe array_filter() function filters elements of an array using a callback function. This method allows for flexible conditions to remove elements. Example: The array_filter() function removes the element "CSS" by returning false for that element in the callback function. PHP <?php $arr = ["HTML", "CSS", "JavaScript", "PHP"]; $remVal = "CSS"; $arr = array_filter($arr, function($value) use ($remVal) { return $value !== $remVal; }); print_r($arr); ?> OutputArray ( [0] => HTML [2] => JavaScript [3] => PHP ) Using array_splice() FunctionThe array_splice() function removes a portion of the array and can be used to remove an element by its key. Example: The array_splice() function removes one element at index 1 ("CSS") from the array. PHP <?php $arr = ["HTML", "CSS", "JavaScript", "PHP"]; $key = 1; array_splice($arr, $key, 1); print_r($arr); ?> OutputArray ( [0] => HTML [1] => JavaScript [2] => PHP ) Using foreach LoopA foreach loop can be used to create a new array without the element to be removed, which is particularly useful for associative arrays. Example: The foreach loop iterates through the array and adds elements that do not match "CSS" to a new array. PHP <?php $arr = ["HTML", "CSS", "JavaScript", "PHP"]; $remVal = "CSS"; $newArr = []; foreach ($arr as $value) { if ($value !== $remVal) { $newArr[] = $value; } } print_r($newArr); ?> OutputArray ( [0] => HTML [1] => JavaScript [2] => PHP ) Comment More infoAdvertise with us Next Article Remove Specific Element from an Array in PHP B blalverma92 Follow Improve Article Tags : PHP Similar Reads Remove First Element from an Array in PHP Given an array, the task is to remove the first element from an array in PHP. Examples:Input: arr = [1, 2, 3, 4, 5, 6, 7]; Output: 2, 3, 4, 5, 6, 7 Input: arr = [3, 4, 5, 6, 7, 1, 2] Output: 4, 5, 6, 7, 1, 2Below are the methods to remove the first element from an array in PHP:Table of ContentUsing 3 min read Removing Array Element and Re-Indexing in PHP In order to remove an element from an array, we can use unset() function which removes the element from an array, and then use array_values() function which indexes the array numerically automatically. Function Usedunset(): This function unsets a given variable. Syntax:void unset ( mixed $var [, mix 2 min read PHP | Remove duplicate elements from Array You are given an Array of n-elements.You have to remove the duplicate values without using any loop in PHP and print the array. Examples: Input : array[] = {2, 3, 1, 6, 1, 6, 2, 3} Output : array ( [6] => 2 [7] => 3 [4] => 1 [5] => 6 ) Input : array[] = {4, 2, 7, 3, 2, 7, 3} Output : arr 3 min read JavaScript - How to Remove an Element from an Array? Removing elements from an array is a fundamental operation in JavaScript, essential for data manipulation, filtering, and transformation. This guide will explore different methods to efficiently remove elements from an array, enhancing your understanding and capability in handling arrays.1. Using po 3 min read How to delete an Element From an Array in PHP ? To delete an element from an array means to remove a specific value or item from the array, shifting subsequent elements to the left to fill the gap. This operation adjusts the array's length accordingly, eliminating the specified element.This article discusses some of the most common methods used i 4 min read PHP | Program to delete an element from array using unset() function Given an array of elements, we have to delete an element from the array by using the unset() function. Examples: Input : $arr = array("Harsh", "Nishant", "Bikash", "Barun"); unset($arr[3]); Output : Array ( [0] => Harsh [1] => Nishant [2] => Bikash ) Input : $arr = array(1, 2, 6, 7, 8, 9); 2 min read How to get elements in reverse order of an array in PHP ? An array is a collection of elements stored together. Every element in an array belong to a similar data type. The elements in the array are recognized by their index values. The elements can be subjected to a variety of operations, including reversal. There are various ways to reverse the elements 4 min read How to find the index of an element in an array using PHP ? In this article, we will discuss how to find the index of an element in an array in PHP. Array indexing starts from 0 to n-1. Here we have some common approachesTable of ContentUsing array_search() FunctionUsing array_flip()Using a custom functionUsing a foreach LoopUsing array_keys FunctionUsing ar 6 min read How to remove specific elements from the left of a given array of elements using JavaScript ? In this article, we will learn How to remove specific elements from the left of a given array of elements using JavaScript. We have given an array of elements, and we have to remove specific elements from the left of a given array. Here are some common approaches: Table of Content Using splice() met 2 min read How to Switch the First Element of an Arrays Sub Array in PHP? Given a 2D array where each element is an array itself, your task is to switch the first element of each sub-array with the first element of the last sub-array using PHP.Example:Input: num = [ ['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']];Output: [ ['g', 'b', 'c'], ['d', 'e', 'f'], ['a', 'h', ' 2 min read Like