PHP array in array: an example

PHP is a modern programming language, popular and widely used. Like others, it has syntax and semantics. As it is supposed to tools from the sphere of Internet technologies: in PHP, any variable can change its type as and when the developer wishes.

Php array in array

At the time of classical statics, especially when all variables needed to be described in advance, programmers did not even thinkuse a language outside of its syntax.

Today in PHP an array without explicitly specifying the type of its elements (including an array or an object with an unlimited nesting level) -this is reality, Consequently:The syntax and semantics of the language can be interpreted at its discretion.

Basic concepts

Since you don't need to describe anything, an array can appear in an empty space:

  • $ aMassa [] = 'the first value is a string';
  • $ aMassa [] = 123; // the second value is a number.

In other words, to PHPadd to the array, it is not at all necessary to preface this action with a descriptive construction:

  • public$ a_insarray

this is an arrayyou can immediately write:

  • $ thisa_insscDocxS_tr ($ cLine$ oDocx

and everything will be absolutely right.

However, in PHP, deleting an array element is the same as deleting a variable.

  • unset ($ aMasse ['elementName']); // delete the array by key;
  • unset ($ aMassa [1]); // delete the second element of the array;
  • unset ($ aMassa); // delete the entire array.

This is somewhat unusual, similar to deleting a regular variable.

Regular and associative arrays

According to PHP syntax:array keys are always a sequence of integers. This statement is always true, but it is not always true that this sequence is continuous.

PHP array keys

When an associative array in PHP became a commonplace phenomenon, the relationship to keys did not change, just the elements that received the key designated by the programmer (anything but an array or an object) began to have:

  • ordinary key in order;
  • specific key for a particular element of the array.

The language does not limit the programmer in the number of elements and dimensions, but in both cases one should adhere to the visible and understood number. The world is very diverse, but planing arrays of more than two dimensions is an occupation with very dubious semantics. A decent orientation in the number of elements is a thousand or two, but not a million or billion.

"Antimassive" remark

Of course, you can find such tasks, but more often there are such solutions when the program is very similar to the monsters on PL / 1 or Fortran, which forced the processor of large Soviet computers from the EC / IBM series to go numb (freeze).

PHP array to string

You can always do with modest and simple algorithms, even when you need to calculate the flight path to Uranus or Neptune. Fly for a long time, which is tricky with monstrous algorithms armed with space arrays.

Extremes in programming are always dangerous, memory can end at the most unexpected moment, and the logic of PHP does not control memory work for a long time, on closer examination you can detect miscalculations across the whole range of possible situations.

Arrays and strings

The world is always symbols, that is, in the context of a programming language, strings. Even when the program is entirely devoted to mathematical calculations, numbers and numbers are all the same characters.

Turning an array into a string into PHP is easy, just like back. It can even be said that this transformation became a kind of construction, not yet embodied in the syntax of the language, but already introduced as an experience by the functions explode () and implode ().

explode () usage example

Here is the challenge:

  • $ cSel$ oBase-> iLineSel ('d_name, d_work_folder, d_load''rm_documents'

creates a string:

  • "FileName | WorkingFolder | FlagLoad"

which is by design:

  • $ aSelexplode$ cSel

turns into an array:

  • $ aSel [0] = "FileName";
  • $ aSel [1] = "Working Folder";
  • $ aSel [2] = "Flag Download";

Very convenient and practical. The inverse implode () function sticks together all the elements of an array in one line, while not distinguishing between string values ​​and numbers. There will be difficulties with the values ​​of other types (objects, internal arrays), but they can be solved.

PHP: array in array

The practice of using arrays has three characteristic points. A regular array (elements are available by language keys), an associative array (elements are available by programmer keys), and any variants of keys, but objects are values, in the simple case, just other arrays.

Php associative array

It is often necessary to use an array on a PHP array. By and large, an array (especially when it is associative) can be viewed as a way out of the syntax of the language: if a variable is just one value at a time, then an array is a set of values.

An associative array is a mass of variables, the names and values ​​of which are formed in the process of the program, and therefore can be used according to the programmer’s idea, and not the syntax of the language.So the key determines the use of the value. It is important to understand: it is not the key that matches the value, but the “use" of the value. For example, in the switch construction (array [key]) {case 'x1': func1 (); case 'x2': func2 (); ...}.

However, in PHP, an array in an array will always be tightly bound to any use case, since other constructs (operators and expressions) are, by definition, not at all “massive”. In other words, giving the array its own meaning, it will always have to be specified during use.

The situation changes dramatically when there are not arrays, but objects in an array.

Object arrays - dynamics

When an array contains objects as elements, this is a very practical way beyond strict use (that is, “beyond” the syntax).

Php remove array element

An object is a more advanced data structure than an array. Each object can include a number of arrays. Unlike the latter, the object also has methods that are directly related to it and contain the proper functionality. Methods link the internal contents of an object with external events, functions, and other objects.

Objects tend to inherit, the execution of the functional, both from top to bottom and back - it all depends on the logic implemented by the programmer.But in any case, the function-methods of objects can be used instead of any syntactic construction [if () {} else {}; switch () {case '': ... break; case '': ... break; }; while () {}], they can also work as assignment operators and form executable expressions.

Due to inheritance and polymorphism, which is implemented quite fully, you can change the syntaxPHP "array in array" on its own variant "array through the logic of its objects". The latter means an extremely simple, but extremely effective idea. Creating an array of PHP objects: add to array = add abstract element.

PHP add to array

Abstract and real objects

Abstraction in object-oriented programming is rather a tribute to traditions that have changed the face of modern programming. In fact, the initial object is far from abstract and must reliably describe everything that its descendants should have. But since OOP implementation is lame not only in PHP, it’s usually difficult to get by with one abstract element.

Almost always you need to have an abstract element and a system of abstract elements. It is not always possible to inherit a system of objects from an object, just as not all tasks allow a system of objects as an initial object.

By and large, it is possible that, and another, the question is that in each particular case, one should be guided by common sense. The PLO is at the stage of rapid development, has many white spots and fresh ideas. Therefore, combining impromptu, intuition and common sense is the only sensible solution.

Example “Correct Documents”

The library PhpOffice \ PhpWord did not become strongly popular, but gave the opportunity to develop websites that easily manipulate MS Word documents: they can read, process and create them.

It cannot be said that the library is easy to learn, but there is definitely an area for its effective use. The following screenshots of the code show how you can describe a set of arrays and fill them.

Example. Array Description

Filling arrays at the initial stage is as follows: $ aSel array is read from the database tables, converted toPHP array to string, the action occurs on each array function explode ().

Example. Filling arrays

The described is a time-consuming, but quite tangible form of applying arrays to a serious practical problem.

The previously described ideas of using arrays and OOP were expressed when parsing the body of a document, which, as you know, is built according to the OOXML standard.To solve this problem, it is simply impossible to foresee the variation of arrays in arrays for all variants of the document content, but it is very possible to foresee a single document array.

To designate in this array as an element only a paragraph or a table is also available, well, and then the technical matter: the paragraph is divided into styles and texts, and the table into cells in which paragraphs lie.

This solution works, is accessible and tangible. It does not require keeping in mind numerous large arrays and does not force the design to plan the structure of arrays, both when reading a document and when creating it.

Simple, practical, affordable.

Related news

PHP array in array: an example image, picture, imagery

PHP array in array: an example 49

PHP array in array: an example 67

PHP array in array: an example 24

PHP array in array: an example 16

PHP array in array: an example 35

PHP array in array: an example 41

PHP array in array: an example 9

PHP array in array: an example 82

PHP array in array: an example 21