Note that there are some explanatory texts on larger screens.

plurals
  1. POVariable naming for multi dimensional arrays (PHP)
    text
    copied!<p>I'm having a hard time trying to figure out a system of naming multi-dimensional PHP arrays, so that by looking at the variable name, you get a hint about the structure of the multi-dimensional array.</p> <p>A fictional example:</p> <pre><code>$task = array('who'=&gt;'John', 'what'=&gt;'wash the dishes', 'priority'=&gt;10); $calendar[$year][$month][$day][$task_id] = $task; </code></pre> <p>In this case I named it "calendar" because the whole structure has a simple meaning as a whole, but in most cases there is no such direct connection to a single word or even a real-life concept. Also, I used date parts here (year, month, day) for exemplification, but normally my keys are integer database record ids.</p> <p>So I would like a system of naming to describe this relation:</p> <pre><code>"year X month X day -&gt; list of tasks" </code></pre> <p>more generally:</p> <pre><code>"key1 x key2 x key3 x ... x key n -&gt; items" </code></pre> <p>A possible naming convention, using the word <strong>by</strong> and underscores as separators between keys:</p> <pre><code>$tasks_by_year_month_day = array(...); // items_by_key1_key2_key3 </code></pre> <p>so that, keeping the same convention, I would write:</p> <pre><code>$tasks_by_month_day = $tasks_by_year_month_day['2010']; </code></pre> <p>or</p> <pre><code>$november2010Tasks_by_day = $tasks_by_year_month_day['2010']['nov']; </code></pre> <p>Is there a standard or cleaner way of naming this kind of arrays?</p> <p>Thank you.</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