Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your question is subjective, in that everyone may have a different approach to the situation you stated, and you are wise to even ask the question; How best to name your variables, classes, etc. Sad to say, I spend more time than I care to admit determining the best variable names that make sense and satisfy the requirements. My ultimate goal is to write code which is '<a href="http://en.wikipedia.org/wiki/Self-documenting">self documenting</a>'. By writing self-documenting code you will find that it is much easier to add features or fix defects as they arise.</p> <p>Over the years I have come to find that these practices work best for me:</p> <p><strong>Arrays: Always plural</strong></p> <p>I do this so loop control structures make more semantic sense, and are easier to work with.</p> <pre><code>// With a plural array it's easy to access a single element foreach ($students as $student) {} // Makes more sense semantically do {} (while (count($students) &gt; 0); </code></pre> <p><strong>Arrays of objects > deep multi-dimensional arrays</strong></p> <p>In your example your arrays started blowing up to be 3 element deep multi-dimensional arrays, and as correct as Robbie's code snippet is, it demonstrates the complexity it takes to iterate over multi-dimensional arrays. Instead, I would suggest creating objects, which can be added to an array. Note that the following code is demonstrative only, I always use accessors.</p> <pre><code>class Owner { public $year; public $measure; public $month; } // Demonstrative hydration for ($i = 1 ; $i &lt;= 3 ; $i++) { $owner = new Owner(); $owner-&gt;year = 2012; $owner-&gt;measure = $i; $owner-&gt;month = rand(1,12); $owners[] = $owner; } </code></pre> <p>Now, you only need to iterate over a flat array to gain access to the data you need:</p> <pre><code>foreach ($owners as $owner) { var_dump(sprintf('%d.%d: %d', $owner-&gt;month, $owner-&gt;year, $owner-&gt;measure)); } </code></pre> <p>The cool thing about this array of objects approach is how easy it will be to add enhancements, what if you want to add an owner name? No problem, simply add the member variable to your class and modify your hydration a bit:</p> <pre><code>class Owner { public $year; public $measure; public $month; public $name; } $names = array('Lars', 'James', 'Kirk', 'Robert'); // Demonstrative hydration for ($i = 1 ; $i &lt;= 3 ; $i++) { $owner = new Owner(); $owner-&gt;year = 2012; $owner-&gt;measure = $i; $owner-&gt;month = rand(1,12); $owner-&gt;name = array_rand($names); $owners[] = $owner; } foreach ($owners as $owner) { var_dump(sprintf('%s: %d.%d: %d', $owner-&gt;name, $owner-&gt;month, $owner-&gt;year, $owner-&gt;measure)); } </code></pre> <p>You have to remember that the above code snippets are just suggestions, if you rather stick with deep multi-dimensional arrays, then you will have to figure out an element ordering arrangement that makes sense to <strong>YOU</strong> and those you work with, if you think you will have trouble with the setup six months down the road, then it is best to implement a better strategy while you have the chance.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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