Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You're doing it right. You must realize that PHP does not have real multi-dimensional arrays; what you're looking at is an array of arrays, each one-dimensional. The major array is storing the pointers (the "iterator", as you put it). Because of this, row-first is the <em>only</em> reasonable way to go:</p> <p>In your particular example, you can think of your two-dimensional array as containing a collection of objects, each of which has values for <code>'year', 'month',</code> and <code>'measure'</code> (plus the primary key, <code>'owner'</code>). By filling in the major index, you can refer to each row of the two-dimensional array like this: <code>$myArray[$owner]</code>. Each such value is a three-element array with keys <code>'year', 'month',</code> and <code>'measure'</code>. In other words, it is identical to your original, one-dimensional data structure for the same information! You can pass it to a function that deals with just one row of your table, you can easily sort the rows of <code>$myArray</code>, etc. </p> <p>If you were to put your indices the other way around, there's no way you can recover your individual records. There is no "slice" notation that can give you an entire "column" of a two-dimensional array. </p> <p>Now for a bit of broader perspective: </p> <p>Since you asked your question in terms of rows and columns, note that putting the "row" index first makes your arrays compatible with matrix arithmetic. This is an enormous win if you have to do calculations with matrices. Database notation also puts records in rows, so doing it backwards would needlessly complicate things.</p> <p>C has real two-dimensional arrays <em>and</em> arrays of pointers. Arrays of pointers work exactly as in PHP (though only numeric indices are allowed), and for the same reason: the major index selects from an array of pointers, and the minor index is simply the index of the pointed-to array. C's two-dimensional arrays work the same way: The major index is on the left, and adjacent locations in memory differ by one value of the <em>minor</em> (second) index (except at the end of a row, of course). This makes them compatible with pointer arrays, since it possible to reference a row of a two-dimensional array by using a single index. For example, <code>a[0]</code> is <code>abcd</code>:</p> <pre><code> a[.][0] a[.][1] a[.][2] a[.][3] a[0]: a b c d a[1]: e f g . a[2]: . . . . a[3]: . . . . </code></pre> <p>The system works seamlessly because the major (row) index is first. Fortran has real two-dimensional arrays but has the major index on the right: Adjacent locations in memory differ by one value of the <em>left</em> (first) index. I found this a pain in the neck, since there's no subexpression that reduces to a one-dimensional array in the same way. (But I have a C background so I'm certainly prejudiced).</p> <p>In short: You're doing it right, and it's probably not by accident but because you learned by looking at well-written code.</p>
    singulars
    1. This table or related slice is empty.
    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. 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