PHP array: delete element first, last, by value

PHP arrays are used everywhere. Adding and changing values ​​is usually straightforward.

Removing array elements is a special operation. You can simply delete the item, or you can delete and use it. This nuance gives great opportunities.

PHP Arrays

PHP is a modern programming language, functionality in terms of working with arrays is performed at a high level. The programmer has the ability to use regular and associative arrays, design multidimensional data structures, and have any type of value as array elements.

There is a developed set of functions for working with arrays, special syntax constructs. It is possible to bypass the array according to its own algorithm and assign its own processing functions.

Examples of creating and using an array

The scPrint function is helper. It recursively writes an array to a string of characters to demonstrate the results.

The $ aFruits array is created in the usual way: values ​​are listed, indexes are assigned automatically from scratch. The last comma is irrelevant and does not create another empty element.

The $ aData array is created empty, then values ​​are entered into it. Three - automatically, and two - with associative indices, which do not affect the general numbering of values. So, the elements "drain" and "peach" have the indices "new" and "fresh", respectively.

The $ aInfo array is multidimensional and associative.

PHP array creation

How to delete an element in a PHP array, three delete operations show.

The first operation removes the second element from the $ aFruits array, its index is 1. It should be noted that the indices following it are not shifted, which means that in cyclic operations with such an array it is necessary to check the existence of the element.

The second operation - deletes the last and first elements in the $ aData array, which confirms the absence of the effect of deletion on indices and the possibility of simultaneous deletion of several elements.

Third - deletes the array in the array and the element in the array that is part of another array.

Normal element deletion - unset

The unset function deletes. No matter what. It can be just a variable or an element of an array. It is believed that unset () is a language operator, not a function. This operator does not return any value, but it "destroys" what is passed to it as parameters. The variable or array disappears as if they weren't there.

Removing an item - unset

In PHP, you can delete empty array elements in different ways, in fact, what to consider an empty element depends on the programmer. However, it is not too wise to use several parameters for this in the unset () operator. It is more practical to carry out group operations into group functions.

Modern computers work very fast, and PHP is very fast. But this is not a reason to create and process tons of information by cumbersome algorithms, this is an objective reason to approach the process of removing array elements in progressive ways.

Removing items with lowercase methods

In PHP, you can remove empty array elements in bulk by converting the array to a string and returning it back. But this case is suitable only for really empty elements, missing indexes, or for the purpose of reindexing an array.

The concept of an empty element depends on the task. Often an existing array element that contains certain information becomes empty. For example, an array of visitors is kept. The array element contains:

  • visitor arrival time;
  • current operating mode;
  • active page;
  • time of the last action.

If the difference between the time of arrival and the time of the last action is more than 1 minute (or another value), we can assume that the client has left the site. Records about such clients can be deleted if the task is to monitor the list of active visitors and not to use more advanced methods using JavaScript.

However, lowercase processing is good. For example, in PHP you can remove duplicate array elements like this:

Delete items line by line

Fast and affordable way. It is not necessary to use the characters '[' and ']' to denote each element, but remember that when transforming an array into a string, you must comply with the requirement of uniqueness of each element. Symbols for the frame should be selected taking into account the characters that are valid in the element. An unshakable rule: each element of the array in a string is unique and has its own place (otherwise nothing can be returned back).

This method is more convenient when there is a task in PHP to remove an element of an array by value. You can use the array_flip function and swap values ​​and keys, then make a classic unset. You can use the array_search function and find the key of the value you want to delete. But the lowercase solution is clearer and simpler.

About Dimensions and Sizes

PHP practically does not limit the developer in anything: neither in the number of dimensions, nor in the sizes of elements. There is no sense to get involved in this. Each element should be the shortest possible length, and the number of dimensions should tend to unity.

If the number of dimensions of the array is more than three, this is a good reason to reconsider the decision. If the array element has a length of more than 4000-8000 characters, there should be doubts about the reasonableness of the constructed data picture.

About Dimensions and Sizes

This opinion does not follow from the context of the functionality of the PHP array: delete an element, add an object of a different type, change something from one to something completely different. Simplicity is the key to success not only in life, but also in the algorithm. The program should work, and not surprise you with its dimensions, dimensions and scale of ideas. The result is important, not the grand idea.

PHP stack idea

As a modern programming language, PHP does not pass by recursion and stack. It doesn’t matter at all what the programmer means when he uses the array_pop () function in PHP: delete the last element of the array or just get it in some variable.

But it should be borne in mind that in this context, the array_pop function is from the sphere of push & pop, that is, these are tools of the stack, not deletion.

PHP stack idea

It is customary to say here not “delete”, but “extract”. Semantics are significantly different. However, the array_shift () function in PHP: deleting the first element of an array or extracting it - has a different connotation. Here, the element is also extracted into an external variable, and it will not be in the array, but the indices are shifted.

When extracting the first element from the array, all elements following it are shifted forward, but only numeric indices change, lowercase ones remain unchanged.

Delete or change: transaction history

A variable is a very long past, an array - it has long been, an object - it was yesterday. Object-oriented programming is still being talked about, but they are not using anything to their full potential. A rare case when surface decisions have become the subject of enthusiastic decisions and the "sophisticated" mass of the "body" of site management systems (CMS).

Modern CMS

Objective rule: it’s not the quantity of code that matters, but its quality! But no modern CMS has ever listened to this rule. Its authors believe that they are doing the right thing and know what they are doing.

Result (characteristic feature): none of the modern CMS is notable for a decent “figure” (harmony and ease of construction), everyone has an immense completeness of the code, each requires respect:

  • highly qualified programmer;
  • needs installation;
  • makes hosting requirements;
  • creates difficulties when moving to another hosting;
  • really slows down in work and administration.

Programmers have been moving towards the concept of rollback for a very long time, modern programming does not think of creating software without two functions:

  • undo;
  • redo.

It’s not only human nature to make mistakes, but in any situation there should be a rollback. In modern means of Internet programming to this day, this moment is not only irrelevant, but also used on a very limited scale.

PHP operations on an array: delete an element, change its type or add something new are understandable. But before there were variables, then arrays, then objects. Isn't there a reason to think that an array is just a variable over time?

An array is a data structure over time. To this day, not a single language considers time as a factor of syntax. You can’t even talk about semantics: from ancient times to this day, programmers and users understand only files and folders. The maximum development has reached, for example, in PHP the namespace (namespace) is trivially reflected in the structure of folders and files.

In this context, banal actions in PHP on an array: delete an element, change or add - require additional actions from the programmer. You can leave everything as it is, and it will turn out, as always. You can take into account each operation on the data and record it in full, create and store a history of operations.

Operations history

This will be a completely different level of work and radically better quality of the result.


All Articles