{"id":1299,"date":"2018-01-19T19:22:40","date_gmt":"2018-01-19T19:22:40","guid":{"rendered":"http:\/\/goofy-trucks.flywheelsites.com\/arrays-101\/"},"modified":"2018-01-19T19:24:40","modified_gmt":"2018-01-19T19:24:40","slug":"arrays-101","status":"publish","type":"post","link":"https:\/\/phpbuilder.com\/arrays-101\/","title":{"rendered":"Arrays 101"},"content":{"rendered":"<div class=\"phpbuilder-content\">\n<div class=\"phpbuilder-meta\">\n<div class=\"\">By Joshua Petrovich<\/div>\n<div class=\"\">on April 24, 2003<\/div>\n<\/p><\/div>\n<div id=\"overflow-content\">\n<div class=\"articlePara\">\nIf you&#8217;ve ever created large scripts that require many variables (sometimes nearly 100), you know<br \/>\nwhat it&#8217;s like trying to keep track of what each variable contains and what it&#8217;s used for. Trust me,<br \/>\nI&#8217;ve been there. If we can store those variables inside <i>other<\/i> variables then the list of hundereds<br \/>\nshrinks to less than 10. That&#8217;s where arrays come in.\n<\/div>\n<div class=\"articlePara\">An array, broken down to its simplest form, is simply a variable that holds<br \/>\nvariables. It&#8217;s kind of like a bunch of houses in a city. The city contains many houses and each house<br \/>\nhas an address. In the same manner, each variable (house) within the array (city) has its own address<br \/>\ncalled an <i>index<\/i>.\n<\/div>\n<div class=\"articlePara\">Let&#8217;s say you have the names of three persons (John, Susie, and Dave)<br \/>\nstored in the variables named <code class=\"example\">$sPerson1<\/code>, <code class=\"example\">$sPerson2<\/code>,<br \/>\nand <code class=\"example\">$sPerson3<\/code> respectively. Now you can use<br \/>\nthose three variables within your program, but it&#8217;s easy to lose track of which one is which&#8230;<br \/>\nespecially if you have other variables. To compact the three variables into an array, you&#8217;d do something like this:\n<\/div>\n<div class=\"articlePhpEx\">\n<font face=\"courier\"><code><span style=\"color: #000000\"><\/p>\n<p><span style=\"color: #0000BB\">&lt;?php<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0$arrayPeople\u00a0<\/span><span style=\"color: #007700\">=\u00a0array(<\/span><span style=\"color: #DD0000\">\"John\"<\/span><span style=\"color: #007700\">,\u00a0<\/span><span style=\"color: #DD0000\">\"Susie\"<\/span><span style=\"color: #007700\">,\u00a0<\/span><span style=\"color: #DD0000\">\"Dave\"<\/span><span style=\"color: #007700\">);<br \/>\n<br \/><\/span><span style=\"color: #0000BB\">?&gt;<br \/>\n<br \/><\/span><br \/>\n<\/span><br \/>\n<\/code><\/font><\/div>\n<div class=\"articlePara\">\nNow instead of using <code class=\"example\">$sPerson1<\/code>, <code class=\"example\">$sPerson2<\/code>,<br \/>\nand <code class=\"example\">$sPerson3<\/code> I can use <code class=\"example\">$arrayPeople<\/code>. Note<br \/>\nhow I created the array using the <code class=\"example\">array()<\/code> function included with PHP.<br \/>\nIf those three names were numbers instead, I wouldn&#8217;t surround the number with quotes. So now to print<br \/>\nthose three names I&#8217;d do this:\n<\/div>\n<div class=\"articlePhpEx\">\n<font face=\"courier\"><code><span style=\"color: #000000\"><\/p>\n<p><span style=\"color: #0000BB\">&lt;?php<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0$arrayPeople\u00a0<\/span><span style=\"color: #007700\">=\u00a0array(<\/span><span style=\"color: #DD0000\">\"John\"<\/span><span style=\"color: #007700\">,\u00a0<\/span><span style=\"color: #DD0000\">\"Susie\"<\/span><span style=\"color: #007700\">,\u00a0<\/span><span style=\"color: #DD0000\">\"Dave\"<\/span><span style=\"color: #007700\">);<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0print\u00a0<\/span><span style=\"color: #0000BB\">$arrayPeople<\/span><span style=\"color: #007700\">[<\/span><span style=\"color: #0000BB\">0<\/span><span style=\"color: #007700\">];<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0print\u00a0<\/span><span style=\"color: #0000BB\">$arrayPeople<\/span><span style=\"color: #007700\">[<\/span><span style=\"color: #0000BB\">1<\/span><span style=\"color: #007700\">];<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0print\u00a0<\/span><span style=\"color: #0000BB\">$arrayPeople<\/span><span style=\"color: #007700\">[<\/span><span style=\"color: #0000BB\">2<\/span><span style=\"color: #007700\">];<br \/>\n<br \/><\/span><span style=\"color: #0000BB\">?&gt;<br \/>\n<br \/><\/span><br \/>\n<\/span><br \/>\n<\/code><\/font><\/div>\n<div class=\"articlePara\"><i>Why did he start with zero?<\/i> Because that&#8217;s where the index starts. Whatever<br \/>\ngets put into the array is assigned an index of zero(0) and that number automatically increments from there.<br \/>\nYou can manually assign an index for a particular entry, but I&#8217;ll cover that later. For now, I&#8217;ll show you<br \/>\nhow to use a loop to automatically print the contents of an entire array:\n<\/div>\n<div class=\"articlePhpEx\">\n<font face=\"courier\"><code><span style=\"color: #000000\"><\/p>\n<p><span style=\"color: #0000BB\">&lt;?php<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0$arrayPeople\u00a0<\/span><span style=\"color: #007700\">=\u00a0array(<\/span><span style=\"color: #DD0000\">\"John\"<\/span><span style=\"color: #007700\">,\u00a0<\/span><span style=\"color: #DD0000\">\"Susie\"<\/span><span style=\"color: #007700\">,\u00a0<\/span><span style=\"color: #DD0000\">\"Dave\"<\/span><span style=\"color: #007700\">);<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #0000BB\">$nArraySize\u00a0<\/span><span style=\"color: #007700\">=\u00a0<\/span><span style=\"color: #0000BB\">count<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$arrayPeople<\/span><span style=\"color: #007700\">);<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0for(<\/span><span style=\"color: #0000BB\">$index<\/span><span style=\"color: #007700\">=<\/span><span style=\"color: #0000BB\">0<\/span><span style=\"color: #007700\">;\u00a0<\/span><span style=\"color: #0000BB\">$index\u00a0<\/span><span style=\"color: #007700\">&lt;\u00a0<\/span><span style=\"color: #0000BB\">$nArraySize<\/span><span style=\"color: #007700\">;\u00a0<\/span><span style=\"color: #0000BB\">$index<\/span><span style=\"color: #007700\">++)\u00a0<\/span><span style=\"color: #FF8000\">\/\/\u00a0max.\u00a0index\u00a0is\u00a0always\u00a0number\u00a0of\u00a0entries\u00a0-\u00a01<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/\u00a0because\u00a0index\u00a0starts\u00a0at\u00a0zero<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #007700\">{<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0print\u00a0<\/span><span style=\"color: #0000BB\">$arrayPeople<\/span><span style=\"color: #007700\">[<\/span><span style=\"color: #0000BB\">$index<\/span><span style=\"color: #007700\">];<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0}<br \/>\n<br \/><\/span><span style=\"color: #0000BB\">?&gt;<br \/>\n<br \/><\/span><br \/>\n<\/span><br \/>\n<\/code><\/font><\/div>\n<div class=\"articlePara\">\nIn this case, <code class=\"example\">$index<\/code> is the index (address) for the entry and<br \/>\n<code class=\"example\">$nArraySize<\/code> is the number of entries in the array. The count()<br \/>\nfunction returns the number of entries in the array. Now for very small array like the one I just<br \/>\nused, using a loop actually uses more code, but when you start dealing with arrays that have hundereds<br \/>\nor even thousands of entries (they do exist), you&#8217;ll be glad that you&#8217;re using a loop.\n<\/div>\n<div class=\"articlePara\">This is where I talk about creating your own indexes for array entries.<br \/>\nWhenever I use SESSIONS for administrator access areas of my website, I use an array to store<br \/>\nsession information. Here&#8217;s what the code looks like:<\/div>\n<div class=\"articlePhpEx\">\n<font face=\"courier\"><code><span style=\"color: #000000\"><\/p>\n<p><span style=\"color: #0000BB\">&lt;?php<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0$SESSION<\/span><span style=\"color: #007700\">=\u00a0array();\u00a0<\/span><span style=\"color: #FF8000\">\/\/\u00a0that\u00a0creates\u00a0a\u00a0blank\u00a0array<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #0000BB\">$SESSION<\/span><span style=\"color: #007700\">[<\/span><span style=\"color: #DD0000\">\"username\"<\/span><span style=\"color: #007700\">]\u00a0=\u00a0<\/span><span style=\"color: #0000BB\">$sUserName<\/span><span style=\"color: #007700\">;<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #0000BB\">$SESSION<\/span><span style=\"color: #007700\">[<\/span><span style=\"color: #DD0000\">\"password\"<\/span><span style=\"color: #007700\">]\u00a0=\u00a0<\/span><span style=\"color: #0000BB\">$sPassword<\/span><span style=\"color: #007700\">;<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #0000BB\">$SESSION<\/span><span style=\"color: #007700\">[<\/span><span style=\"color: #DD0000\">\"accesslevel\"<\/span><span style=\"color: #007700\">]\u00a0=\u00a0<\/span><span style=\"color: #0000BB\">$nLevel<\/span><span style=\"color: #007700\">;<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0<\/span><span style=\"color: #FF8000\">\/\/\u00a0etc,etc,etc.<br \/>\n<br \/><\/span><span style=\"color: #0000BB\">?&gt;<br \/>\n<br \/><\/span><br \/>\n<\/span><br \/>\n<\/code><\/font><\/div>\n<div class=\"articlePara\">You see how I used words to identify the index? This way I know that<br \/>\n<code class=\"example\">$SESSION[\"username\"]<\/code> contains the username of the person.<br \/>\nIt&#8217;s a lot easier than trying to remember that <code class=\"example\">$SESSION[0]<\/code> contains<br \/>\nthe username. My way of coding arrays like this is to use the name of the variable as the index<br \/>\nfor that entry. So to store <code class=\"example\">$nDaysinMay<\/code> in array<br \/>\n<code class=\"example\">$arrayDays<\/code> I&#8217;d put it in <code class=\"example\">$arrayDays[\"nDaysinMay\"]<\/code>.<br \/>\nThis way I can even keep track of which variable that entry contains. Now this is where I can dive into multi-dimensional arrays.\n<\/div>\n<\/div>\n<p><\/p>\n<div style=\"float: left; padding:15px; color:#17AAF3\">\n<div style=\"background-color:#B6E5FC; font-size:16px; margin-top:1px; padding:1px 4px 1px 4px; color:#000; font-style:bold; float:left;\">1<\/div>\n<div style=\"float:left; font-size:16px; color:#FF7A22; padding:2px 2px 2px 2px; \">| <\/div>\n<div style=\"float:left; padding:2px 4px 2px 4px;\"><a class=\"pageNumber\" href=\"petrovich200304244658.html?page=2\">2<\/a> <\/div>\n<div style=\"float:left; padding:2px;\"><a class=\"paginationPageLink\" href=\"petrovich200304244658.html?page=2\">Next Page \u00bb<\/a><\/div>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>If you&#8217;ve ever created large scripts that require many variables (sometimes nearly 100), you know what it&#8217;s<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1299","post","type-post","status-publish","format-standard","hentry","category-tutorials"],"_links":{"self":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1299","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/comments?post=1299"}],"version-history":[{"count":1,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1299\/revisions"}],"predecessor-version":[{"id":3191,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1299\/revisions\/3191"}],"wp:attachment":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/media?parent=1299"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/categories?post=1299"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/tags?post=1299"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}