What is the important function array in PHP

PHP

Hand-bound notebooks

Health reasons force - and bring (!) - me to changes that were never planned: Instead of variables, loops and queries, I now work with wonderful paper: wafer-thin tracing paper, matt white writing paper, rustic kraft paper, flexible, black cardboard, solid Gray board and many more. All of them are used to create hand-bound notebooks, diaries and notebooks that are fun to look at - just imagine how great it is to write in them too!

PHP for you will of course remain in its current form and maybe we'll see each other at Unkelbach Art, on Facebook or Instagram or you can get one of my products. It would be my great pleasure!
More on this...

Save a lot ...

... in a "variable" - the array

What is an array anyway? Well, I would describe an array as a variable that can store many more variables. Each of these "variables" can be addressed by a unique number or a term.

An array is assigned a name like every "normal" variable, but the content is not assigned via a simple equals, but via the function array. In this case there is an array named flowers in which various flower names have been written. These flower names have now automatically been assigned numbers, with the numbering at 0 begins (so rose has the value 0, tulip has the value 1, and so on).

Access is now via the array name plus the number of the entry in square brackets. In this case the term Sunflower issued.

In some cases, however, the default numbering cannot be used and you can also define a unique name (or number) for each entry yourself. First write the name for the value and the value separated by an arrow (=>). "rose", "pink" => "tulip", "white" => "carnation", "yellow" => "sunflower");?> Now everyone has Flower names are assigned a color that can be used to address them. gives tulip out.

If you use words, you have to put them in quotation marks - but if you assign numbers, this is not necessary.

With arrays there is a function to check how many values ​​are in the array. This can be B. can be used if you only want to write a certain number of values ​​in the array. The command to do this is count. The return value would be 4 in our case, since there are four values ​​in the array.

Another important function is to check whether a certain value is already in the array (e.g. I programmed a random generator for lottery numbers and wrote all the numbers in an array and always asked beforehand whether the number does not already exist no value occurs twice).

The function for this is in_array and includes which value is searched for and in which (in which array) it is to be searched. It makes most sense to use it in conjunction with an if query. This checks whether the value tulip in the array flowers is located. If this is the case, the text is output - otherwise nothing happens.

Another feature I used on the lottery random number generator mentioned above was array_push. The array is expanded by a value - it is written to the array. The assignment is done like this: First you specify which array is to be expanded and then specify the desired values ​​(separated by commas).

Note that you cannot assign a name for a value here. If you use this function in conjunction with an array in which you have assigned names, the array will not be expanded.

What is still of interest with arrays is the sorting of the values ​​they contain. This can be done in different ways.

Would you like that Field names sort using the functions ksort or krsort, in which ksort yields an ascending order (a, b, c, d) and krsort a descending one (d, c, b, a). Results in the order yellow (sunflower), pink (tulip), red (rose), white (carnation). However, the values ​​are still addressed in the same way ($ arrayname [field name]).

If you want to sort the values, there are the functions asort and arsort. Also sorted here asort ascending and arsort descending. The sequence here is carnation, rose, sunflower, tulip. Even in the case of an array to which no field names have been assigned, the numbers of the individual values ​​remain the same (Carnation still has entry number 2).

But if everything stays the same now - why do you need sorting?

Well, there is also a possibility of not addressing a specific value of an array, but of having the entire array output at once. This is what the function is for foreach.

With foreach each value of the array is temporarily assigned to a variable and then z. B. listed. The name of the variable is given by the command as specified. ";?> The list would then look like this:

rose
tulip
clove
Sunflower


If I apply my sort order now, the order of the output changes ... ";?> ... results in the following ...

clove
rose
Sunflower
tulip

You may have noticed that here we are in contrast to the if statements did not use curly brackets. This works in this case because we're only running a single statement (the echo statement). For example, if we want to write the line break in an extra echo statement, we have to use curly brackets to define the statement. ";}?> This statement delivers exactly the same result as above, but the spelling is a little different.

This system can also be used to manage if queries within a foreach loop (e.g. if you want to check whether a certain value should be output.

By the way, you can find the mentioned lottery random number generator in the book.

And next, I'll finally reveal the "trick" with the correctly formatted date.