Last Updated:

Deleting Array Elements in php

Deleting Array Elements in php

Considering any programming language, it is worth paying attention to the fact that among the most commonly used, necessary and important structures, arrays occupy a special place. For a programmer, it is very important and useful not only to know how to create, but also how to manage arrays. In this case, one of the most important actions is the process of deleting individual elements of the array in php.

There are several ways to implement this action. Here it is worth proceeding from what information is known about the item being deleted. It is very important to pay attention to whether the identifier or key of the element is known, or whether there is information only about its value.

Features of deleting an array by key

 

The simplest task will be to delete the elements of the array if the key of the element itself is known. By using a function such as unset, it is possible to delete any variable or an individual element of an array. At the same time, the computer's memory, which was used for a specific variable, is additionally cleared.

 

For example, if you have an array:

 

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

In order to delete an element that has a value of 4, you need to apply the following code:

unset($myArr[3]);

Here it is worth paying attention to the fact that it is not the element with a value of 4 that is indicated, but the key of the element (3), which in the presented case will begin with 0. This suggests that in the event that you need to delete the first element, in the program code it will appear as a zero element.

After several elements are implemented simultaneously, 3 elements will be included in the array after performing this action. All keys of those items that remain will not be changed and will be automatically saved. But this option may not always be suitable, since the number of the element that needs to be removed is not always known.

Considerations for deleting an array element by using its value

In the event that nothing is known about the key of a particular element of the array, and there is data only about its value, in such a situation you will have to carry out the process of sorting through all the elements and look for the one that is necessary. For example, if you need to delete an array element with a value of (2), the program code will look like this:

$myArr = array(1,2,3,4,5);
foreach($myArr as $key => $item){
if ($item == 2){
unset($myArr[$key]);

}
}

Pay special attention to the fact that the array condition is checked against the value that is available in the loop in the variable $item. In the event that the deletion process occurs, the variable $key is mandatory, or more precisely, the key of the element.

The use of such an algorithm is possible not only when it is necessary to delete one element, but also in the case when you need to delete a whole array of elements at once. This is because this method provides a condition under which it is possible to determine which of the elements should be removed.

Intersecting array elements and their deletion

 

Among the most convenient tools provided in php is the ability to form an array in which the elements of one array do not intersect with the elements of another. To carry out this process, a special array diff() function is used.

In it, the quality of the parameters are arrays, with which you can perform the action of returning a new array. For example, if you have a source array $myArr from which you need to take only those elements that are not part of the $myArr 2 array. The code snippet will look like this:

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

In the event that you use the array diff() function, it is possible to form a complete new array, which will include only those elements that are necessary:

$resArr = array_diff($myArr, myArr2);

At the same time, you need to pay attention to the fact that despite the fact that the process of forming a new array is taking place, the keys are also stored in parallel. In addition, the sampling process is carried out by using only the first array.

Delete the first element in the php array

 

In the event that in the process of working with the array there is no specific data about the keys of the elements, but you know for sure that there is a need to delete the first element, in this situation you can use a special function array_shift(). Through its use, each programmer has the opportunity to extract the value of the first element, delete it, as well as in the subsequent creation of a new numbering, starting from scratch.

<?php
$stack = array(3 => 'a', 5 => 'b', 'third element' => 'c');
$elm = array_shift($stack);
print_r($stack);
//see on the screen : Array ( [0] => b [third element] => c )
?>

As a result, the first element will be deleted, and all other components will fall under the process of creating a new numbering. It is worth paying attention to the fact that elements that have string keys will not be renamed.

Delete the last element in the php array

 

If there is a need to remove the last element that is part of the array, this is done in this way. To do this, a special function called array_pop() is used in arrays. The code snippet will look like this:

<?php
$stack = array(3 => 'a', 5 => 'b', 'third element' => 'c');
$elm = array_pop($stack);
print_r($stack);
//see on screen : Array ( [3] => a [5] => b )
?>

After the last element from the available array is removed, a process such as renumbering will not take place. This is due to the fact that the removal occurs precisely the last element, which does not displace the other components in front of it.