Home > Article > Backend Development > A must-read for beginners: Introduction to PHP array elements_PHP Tutorial
After a long period of development of PHP, many users know PHP very well. Here I will express my personal understanding and discuss with you about PHP array elements. If you've ever written a script that uses a lot of variables (sometimes nearly 100), you know how frustrating it can be to keep track of what each variable does and what it does. Really, I've had that experience. If we can save the variable inside another variable, the length of the variable list is reduced from 100 to less than 10. This is how arrays came about.
<ol class="dp-xml"><li class="alt"> <span><strong><font color="#006699"><span class="tag"><?</SPAN><SPAN class=tag-name>php</SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute-value><FONT color=#0000ff>array</FONT></SPAN><SPAN class=attribute><FONT color=#ff0000>arrayPeople</FONT></SPAN><SPAN> = array("John", "Susie", "Dave"); </SPAN></SPAN><LI class=alt><SPAN></SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></span></font></strong></span><span> </span> </li></ol>
Now, I use $arrayPeople instead of $sPerson1, $sPerson2, and $sPerson3. Notice how I'm using the array() function in PHP. If the three names were numbers, I wouldn't use quotes around it. To display these three names, I did this:
<ol class="dp-xml"><li class="alt"> <span><strong><font color="#006699"><span class="tag"><?</SPAN><SPAN class=tag-name>php</SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute-value><FONT color=#0000ff>array</FONT></SPAN><SPAN class=attribute><FONT color=#ff0000>arrayPeople</FONT></SPAN><SPAN> = array("John", "Susie", "Dave"); </SPAN></SPAN><LI class=alt><SPAN>print $arrayPeople[0]; </SPAN><LI class=""><SPAN>print $arrayPeople[1]; </SPAN><LI class=alt><SPAN>print $arrayPeople[2]; </SPAN><LI class=""><SPAN></SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></span></font></strong></span><span> </span> </li></ol>
Why start from scratch? Because the index starts there. No matter what you put into the array, the index always starts at zero (0) and automatically accumulates. You can manually assign a specific entry to the index, but I'll get to that in a moment. Now I will show you how to automatically display the contents of an array through a loop:
<ol class="dp-xml"><li class="alt"> <span><strong><font color="#006699"><span class="tag"><?</SPAN><SPAN class=tag-name>php</SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute-value><FONT color=#0000ff>array</FONT></SPAN><SPAN class=attribute><FONT color=#ff0000>arrayPeople</FONT></SPAN><SPAN> = array("John", "Susie", "Dave"); </SPAN></SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>nArraySize</FONT></SPAN><SPAN> = </SPAN><SPAN class=attribute-value><FONT color=#0000ff>count</FONT></SPAN><SPAN>($arrayPeople); </SPAN></SPAN><LI class=""><SPAN>for($</SPAN><SPAN class=attribute><FONT color=#ff0000>index</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>0</FONT></SPAN><SPAN>; $index </SPAN><SPAN class=tag><STRONG><FONT color=#006699><</FONT></STRONG></SPAN><SPAN> $nArraySize; $index++) // max. index is always number of entries - 1 </SPAN></SPAN><LI class=alt><SPAN>// because index starts at zero </SPAN><LI class=""><SPAN>{ </SPAN><LI class=alt><SPAN>print $arrayPeople[$index]; </SPAN><LI class=""><SPAN>} </SPAN><LI class=alt><SPAN></SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></span></font></strong></span><span> </span> </li></ol>
In this case, $index is the index (address) of the entry and $nArraySize is the PHP array element number. The count() function returns the number of elements in a PHP array. For small arrays like the ones I just worked with, using loops does increase the length of the code, but when you start dealing with arrays with hundreds or thousands of elements (and they do exist), you'll be happy to use loops.
Below I will talk about how to create your own index for an array. Whenever I use SESSIONS to set admin rights for my site, I use an array to hold the session information. Here is the relevant code
<ol class="dp-xml"><li class="alt"> <span><strong><font color="#006699"><span class="tag"><?</SPAN><SPAN class=tag-name>php</SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>SESSION</FONT></SPAN><SPAN>= </SPAN><SPAN class=attribute-value><FONT color=#0000ff>array</FONT></SPAN><SPAN>(); // that creates a blank array </SPAN></SPAN><LI class=alt><SPAN>$SESSION["username"] = $sUserName; </SPAN><LI class=""><SPAN>$SESSION["password"] = $sPassword; </SPAN><LI class=alt><SPAN>$SESSION["accesslevel"] = $nLevel; </SPAN><LI class=""><SPAN>// etc,etc,etc. </SPAN><LI class=alt><SPAN></SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></span></font></strong></span><span> </span> </li></ol>
See how I use words to represent indexes? In this way, I can know that $SESSION["username"] contains the person's name. This is much easier than trying to remember from $SESSION[0] that it holds the username. When I work with arrays I always use the name of the variable instead of the index to represent the element like this. So to save $nDaysinMay in array $arrayDays, I would use $arrayDays["nDaysinMay"]. This way I always know what variable is contained in the element.