Arrays 101

zhaozj2021-02-16  47

Original: Joshua Petrovich Translation: Easychen If you have written scripts with a large number of variables (sometimes nearly 100), you will know how uncomfortable to track the content and use of each variable. Really, I have had such an experience. If we can save the variable in another variable, the length of the variable list is reduced from 100 to 10. This is how the array comes.

An array, from the simplest form, is a variable that saves variables. This is like a house in a city. The city has many houses, while each house has an address. The same situation, each variable (house) has its own address in an array (city), we call it index.

Let us assume that you have three people named in variables called $ SPERSON1, $ SPERSON2 and $ SPERSON3. Now you can use these three variables in your program, but this is easy to forget which variable is ... especially if there are other variables. To put these three variables in an array, you can do this:

Now, I use $ ArrayPeople instead of $ SPERSON1, $ SPERSON2, And $ SPERSON3. Note how I use the array () function in PHP. If these three names are numbers, I will not use quotes to cause it. In order to display these three names, I did this:

Why start from scratch? Because the index starts there. No matter what you put in the array, the index always begins automatically from zero (0). You can manually assign a specific entry to the index, wait for this. Now I will show you how to display an array through a loop automatic display:

In this case, $ INDEX is an index (address) of the entry (address), $ Narraysize is the number of array elements. COUNT () function returns the number of array elements. For the decimal group I used to use, use the loop to increase the length of the code, but when you start processing an array of elements (they do exist), you will be happy to use loop.

Lower I will talk about how to create an array to create your own index. Whenever I use sessions to set up administrator privileges for my website, I will use an array to save SEESI information. Here is the relevant code:

See how I use words to expire index? This way I can know $ session ["username"] is the name. This is much easier than you have to keep it from $ session [0]. I always use the array to use the name of the variable that uses the variable to represent the elements. So in order to save $ NDAYSINMAY in array $ ARRAYDAYS, I will use $ ArrayDays ["NDAYSINMAY"]. This way I can always know what variables are included in the element. Low we enter the multidimensional array. The multi-dimensional array, it sounds like a science fiction from the fifth year. In fact, the multidimensional array is just an array in arrays. Remember how did I put the variable in the array? Now I am going to dig up to the group. For example, I operate an online bookstore, selling novels, children's books and magazines, and I put these names in an array. Writing a code is like this:

It is already quite compact, but I can also put these three arrays in an array called $ ARRAYINVENTORY. Here is how to implement it with the way I just mentioned:

now I can use for loop? Display them (including the code on the upper side):

Now you have done it. You can create, populate, and use arrays in any of your script. If you have questions about arrays and PHP, you can write to me on the email address on the side. Thank you for reading this article.

转载请注明原文地址:https://www.9cbs.com/read-25656.html

New Post(0)