Anyone who has studied programming at a university knows that teachers strive to provide only basic, basic material for their students. The topic of arrays is also considered, but at later courses. Why? Because arrays are the basis that allows the programmer to work with large volumes of information.
Introduction
Today we begin the topic by introducing a definition of this term. Arrays are elements of a programming environment that are a set of data in the form of a table or row. Imagine a series of random numbers: 1, 6, 2, 4, 8. This will be an array. Each digit written in a line has its own serial number, and this is what allows you to correlate (enter) them with an array in programming.
Record
Let's see how arrays are written in practice. Record, designate arrays - this means to indicate for the program being created their type (what values will be stored in the array) and the number of cells. Sometimes programmers create immense arrays without specifying the exact number of elements, but then when accessing them, you need to be very careful that the program does not go in cycles and does not start accessing empty cells.
- D: array [1..k] of real; - this is how the array is written in Pascal. If you know when creating a program that you will have a maximum of 5 elements, then you can use the entry D: array [1..5] of real;
As you might have guessed, D is a letter meaning the name of the array; real is the type (format) of data that may be contained in the array; array [] is the number of elements in the array.
Appeal
In order to work with an array element, it must be accessed from the program. Arrays are the same numbers or words as any other. In order to work with an array element, you must enter: D [1]. This will allow you to select the first element of the array and conduct operations with it. For instance:
- print (D [1]); - this command allows you to display the value contained in the 1st cell of the array on the user's screen.
It is worth noting that if you are going to carry out mathematical operations with arrays, then you should pay attention to the type. You can only do this if you have an array of numbers. To make it clearer:
- If you have an array of D: array [1..k] of text; - and in cell D [1] = 1, then you cannot use this element in mathematical operations, because for the program "1" it will be just the word "one", not a number. So watch out for variables and their types.
If you are planning mathematical operations, or the numbers just need to be stored in the array, it is better to worry about its type in advance and assign it to "real" or "integer".
Table
Let's talk about the space around us. We live in a three-dimensional world, and most objects can be described by 3 parameters: length, width, height. So arrays have a dimension. Two-dimensional arrays are tables with data in which each element is assigned not one sequence number, but two - a row number and a column number. When accessing a two-dimensional array, you need to specify both numbers - D [1; 1].
Accordingly, such an array will be able to store a larger amount of data. Unfortunately, in old programming languages, in most cases, the number of an element of an array can be only digits. Therefore, storing data from large tables becomes very problematic due to the fact that each column of the table will have to create a separate array.
For example, suppose we have a table in which student data is recorded. They indicate: year of birth, last name, class.
1989 | Ivanov | Ivan | 9 |
1988 | Petrov | Peter | 10 |
.... | | | |
Under normal circumstances, we will have to create several arrays, depending on the needs. We can create one two-dimensional array of a numerical type to store the year of birth and the class, and a second array for storing textual information (F.I.). But it is inconvenient. First, the last name and first name may need to be processed separately. Secondly, you can easily get confused when filling an array with a year and a class. Therefore, it will be easier to create 4 separate arrays for each column. Agree, very cumbersome?
Php
PHP arrays can solve the problem mentioned above. The fact is that in this programming language you can specify not only the type of data in the array, but also the type of counter (index). In addition, data of various types can be contained in one array. Creating a one-dimensional array (if you need to take one column):
- $ array = array (1989, 1988, ...);
This is an example of creating a simple array. An index is created automatically and counts from zero. That is, the zero element of the array is 1989, the first is 1988, etc. But what if we need to put the whole table in a multidimensional array? What are multidimensional PHP arrays? These are constructions in which each element is also an array. How to parse the example given to us?
$ table = array (
array (1989, "Ivanov", "Ivan", 9),
array (1988, "Petrov", "Peter", 10),
...
);
What do we have in the end? Before us is an array called $ table, in which the rows correspond to the rows in the presented table. If we talk about the elements of the array, then they will look like this:
- $ table [0; 0] = 1989, $ table [0; 1] = "Ivanov", $ table [0; 2] = "Ivan", $ table [0; 3] = 9.
- $ table [1; 0] = 1988, $ table [1; 1] = "Petrov", $ table [1; 2] = "Peter", $ table [1; 3] = 10.
In this case, the 0 and 3 columns of the array will be numeric, and 1 and 2 will be text. If necessary, you can always convert the necessary data to the desired format and combine the cells.