Note that there are some explanatory texts on larger screens.

plurals
  1. POBest Practice: How to Structure Arrays - Standards and Naming Conventions
    text
    copied!<p>What is the best practice in multidimensional array structure in terms of what elements hold the iterator vs the detail elements?</p> <p>The majority of my programming experience (and I do mainly do it for fun) comes from following tutorials on google, so I apologize in advance if this seems an exceptionally daft question - but I do want to start improving my code.</p> <p>Whenever I have needed to make a multidimensional array, my naming has always placed the counter in the first element.</p> <p>For example, if I have a single dimensional array as follows:</p> <pre><code>$myArray['year']=2012; $myArray['month']='July'; $myArray['measure']=3; // and so on. </code></pre> <p>However, if I wanted to make that same array keep a few owners of history I would add another dimension and format it as follows:</p> <pre><code>$myArray[$owner]['year']=2012; $myArray[$owner]['month']='July'; $myArray[$owner]['measure']=3; </code></pre> <p>Edit: To make sure that my example isn't off-putting or leading in the right direction, I am basically following this structure:</p> <pre><code>$myArray[rowOfData][columnOfData] </code></pre> <p>Now, my question is about accepted convention. Should I instead be doing the following?</p> <pre><code>$myArray['year'][$owner]=2012; $myArray['month'][$owner]='July'; $myArray['measure'][$owner]=3; </code></pre> <p>Edit: using that edit from above, should it be:</p> <pre><code>$myArray[columnOfData][rowOfData] </code></pre> <p>I have searched about array naming conventions, but keep hitting articles arguing about whether to name arrays as plurals or not. The way I have been naming them seems to be more logical and I think it follows a structure that resembles an object better i.e. <code>object-&gt;secondaryLevel-&gt;detail</code> but for all I know I have been doing it ass-about all this time. As I getting more and more into programming, I would prefer to change my habits if they are wrong.</p> <p>Is there an accepted standard or is it just anything goes with arrays? If you were looking at code written by someone else, what format would be expecting? I get that any structure that makes sense/is intuitive is accepted.</p> <p>Also from an iteration point of view, which one of the following is more intuitive?:</p> <pre><code>for($i=0;$i&lt;$someNumber;$i++) { echo $myArray[$i]['year']; // OR echo $myArray['year'][$owner]; } </code></pre> <p>Edit: I did have this post tagged as c# and Java because I wanted to get some opinions outside of just PHP programmers. I think that as arrays are used in so many different languages, it would have been good to get some input from programmers in various langauges.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload