PHP Remove Value From Array by Key, Index, Value

PHP Remove Value From Array by Key, Index, Value

Remove element, value from array in PHP; In this tutorial, you will learn how to remove elements/values, array from array in PHP with example.

When working with arrays in PHP, there are several built-in functions that allow developers to manipulate the data in various ways. One common operation is to remove an element or multiple elements from an array based on a key or index value. This can be achieved using several different PHP functions, depending on the specific use case.

How to Remove elements,values, and array from array in PHP

Here, you will explore some of the most popular ways to remove values from arrays in PHP:

  • Method 1: Using unset() function
  • Method 2: Using array_diff() function
  • Method 3: Using array_splice() function
  • Method 4: Using array_pop() function

Method 1: Using unset() function

The unset() function is used to remove an element from an array. The syntax for using unset() function to remove an element from an array is as follows:

unset($array[index]);

Here, $array is the name of the array, and index is the index of the element you want to remove.

Let’s take an example. Suppose you have an array named $fruits that contains the following elements:

$fruits = array("apple", "banana", "orange", "kiwi", "mango");

Now, let’s say you want to remove the element “kiwi” from the array. You can do so using the following code:

unset($fruits[3]);

After executing this code, the array $fruits will contain the following elements:

array("apple", "banana", "orange", "mango");

Let’s take another example for removing an element from a one-dimensional array by key:

To remove an element from a one-dimensional array by key, you can use the unset() function along with the array key that want to remove. Here’s an example:

$fruits = array('apple' => 1, 'banana' => 2, 'orange' => 3, 'kiwi' => 4);
unset($fruits['orange']);
print_r($fruits);

In this example, you have an array of fruits with each fruit assigned a numeric value. You want to remove the ‘orange’ element from the array by its key, so we use the unset() function and specify the key as the index of the element want to remove. After that, you print the remaining elements in the array using the print_r() function.

Method 2: Using array_diff() function

Another way to remove values from an array is to use the array_diff() function. This function returns an array containing all the values of the first array that are not present in any of the other arrays.

The syntax for using array_diff() function to remove values from an array is as follows:

$new_array = array_diff($array, array(value1, value2, ...));

Here, $array is the name of the array, and value1, value2, etc. are the values you want to remove.

Let’s take an example. Suppose you have an array named $numbers that contains the following elements:

$numbers = array(1, 2, 3, 4, 5);

Now, let’s say you want to remove the values 2 and 4 from the array. You can do so using the following code:

$new_array = array_diff($numbers, array(2, 4));

After executing this code, the array $new_array will contain the following elements:

array(1, 3, 5);

Method 3: Using array_splice() function

The array_splice() function is used to remove a portion of an array and replace it with something else. The syntax for using array_splice() function to remove values from an array is as follows:

array_splice($array, $start, $length);

Here, $array is the name of the array, $start is the index of the element where the removal should start, and $length is the number of elements to remove.

Let’s take an example. Suppose you have an array named $colors that contains the following elements:

$colors = array("red", "green", "blue", "yellow", "pink");

Now, let’s say you want to remove the elements “blue” and “yellow” from the array. You can do so using the following code:

array_splice($colors, 2, 2);

After executing this code, the array $colors will contain the following elements:

array("red", "green", "pink");

Method 4: Using array_pop() function

The array_pop() function is a part of PHP’s array manipulation functions. It removes the last element of an array and returns it. This function modifies the original array and reduces its length by one. If the array is empty, array_pop() returns NULL.

To remove multiple elements from an array, you can call array_pop() multiple times. However, you need to be careful not to call array_pop() more times than the number of elements in the array, as it will result in an error.

Now, let’s look at some examples of using array_pop() to remove values from an array.

Example 1: Removing the last element from an array

Suppose you have an array of fruits, and you want to remove the last fruit from it. You can use the array_pop() function as follows:

$fruits = array('apple', 'banana', 'orange', 'kiwi');
$last_fruit = array_pop($fruits);
echo "Removed fruit: " . $last_fruit . "<br>";
print_r($fruits);

In this example, the array_pop() function removes the last element, which is ‘kiwi’, and returns it. You store the removed value in $last_fruit and display it on the screen. Finally, you print the modified array using the print_r() function, which displays the remaining elements in the array.

Example 2: Removing multiple elements from an array

Suppose you have an array of numbers, and want to remove the last two elements from it. You can use the array_pop() function twice, as follows:

$numbers = array(10, 20, 30, 40, 50);
array_pop($numbers);
array_pop($numbers);
print_r($numbers);

In this example, you call the array_pop() function twice, which removes the last two elements from the array. Finally, you print the modified array using the print_r() function, which displays the remaining elements in the array.

Conclusion

In summary, removing elements from arrays is a common task in PHP programming. Developers can use built-in functions such as array_pop(), array_shift(), and unset() to remove elements from regular arrays, and array_splice() and array_filter() for multi-dimensional arrays. By understanding how to remove elements from arrays, developers can efficiently manipulate data and build robust PHP applications.

Recommended PHP Tutorials

  1. PHP Array: Indexed,Associative, Multidimensional
  2. PHP remove duplicates from multidimensional array
  3. Remove Duplicate Elements or Values from Array PHP
  4. How to Convert String to Array in PHP
  5. Array Push and POP in PHP | PHP Tutorial
  6. PHP Search Multidimensional Array [key and value and return key]

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *