Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the elements of the PHP array

Shulou Source: shulou.com Published: 2022-06-02 19:34:00 10月03日 Update

Editor to share with you what are the elements of the PHP array, I hope you will gain something after reading this article, let's discuss it together!

PHP after a long period of development, many users are very familiar with PHP, here I would like to publish a personal understanding, and discuss with you about PHP array elements. If you've ever written scripts that use a large number of variables (sometimes nearly 100), you know how hard it is to track the content and purpose of each variable. Really, I've had this experience before. If we can save the variable in another variable, the length of the variable list will be reduced from 100 to less than 10. This is where the array comes from.

An array, in its simplest form, is a variable that holds variables. It's a lot like a row of houses in a city. The city has many houses, and each house has an address. Similarly, each variable (house) has its own address in an array (city), which we call an index. Let's assume that you have three names stored in variables called $sPerson1, $sPerson2, and $sPerson3. Now you can use these three variables in your program, but it's easy to forget which variable is which. Especially when there are other variables. To put these three variables in an array, you can do something like this:

Now, I use $arrayPeople instead of $sPerson1, $sPerson2, and $sPerson3. Notice how I use the array () function in PHP. If these three names are numbers, I will not use quotation marks to enclose them. To show these three names, I did this:

Why start from scratch? Because that's where the index starts. No matter what you put in the array, the index always accumulates automatically from zero (0). You can manually specify a specific entry for the index, which I'll talk about in a moment. Now I'll show you how to automatically display the contents of an array through a loop:

In this case, $index is the index (address) of the entry, and $nArraySize is the number of elements in the PHP array. The count () function returns the number of elements in the PHP array. For decimal arrays like the one I just used, using loops does increase the length of the code, but when you start working with hundreds of arrays of elements (they do exist), you'll be happy to use loops.

Next I'll talk about how to create your own index for an array. Whenever I use SESSIONS to set administrator permissions for my site, I use arrays to save seesion information. Here is the relevant code

See how I use words to represent indexes? This way I can know that $SESSION ["username"] contains a person's name. This is much easier than remembering from $SESSION [0] that it saves the user name. When I use an array, I always use the name of the variable instead of the index to represent the element. So to save $nDaysinMay in the array $arrayDays, I'll use $arrayDays ["nDaysinMay"]. So I can always know what variables are contained in the element.

After reading this article, I believe you have a certain understanding of "what are the elements of the PHP array". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

Tags: Array variable element index three name address city house loop number person name code content function situation time user article length Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Redmi Apple MySQL Linux