Multi-dimensional arrays in PHP

Hi,
  In previous post I have said little about functioning of arrays in PHP. We saw how output of an array is changed depending on the way it was defined. While this was simple, it was also powerful enough to have some pretty nifty uses.
  The second thing to keep in mind is that a key in an array can have another array as value. This rises possiblities for multi-dimensional arrays and tree structures. This is where many people get confused. But if we just forget the word 'multi-dimension' an visualize the array itself, it becomes a piece of cake to manipulate it.
  This seems so promising but in-fact is not easy at all. Let's see what I mean by 'visualize'. First of all visualize an indexed array(PERL people call these lists). They are nothing but basic lists of comma separated values. Like (a,b,c). We 'visualize' this as a stack. The first element is at the bottom and then each element on top of it. So in our example we have 'a' at bottom of the stack and 'b' on 'a' and 'c' on top of 'b'. That's our stack.
  Now comes associative array. PERL community considers this as a real array. An example would be (fruit=>apple, toy=>gun, computer=>mouse). Here fruit, toy and computer are indexes or keys and apple, gun and mouse are values belonging to those keys respectively. Visualize this array as 2 column table. The first column is the keys and the second is values. The bottom row has the first key=>value pair i.e. fruit=>apple. Then toy=>gun comes above it and computer=>mouse on top. This makes the understanding fairly easy and gives us some manipulative thoughts.
  Always remember that whenever any array is read by PHP, it is read from bottom. So a foreach loop (also called enhanced for loop) reads an array starting from bottom, i.e. first element.
  Next come the dreaded beasts, the multidimensional arrays. When any value in an array is another array, then its called a multidimensional array. Also keep in mind that the array that is in value can also be another multidimensional array. Let's take and example, (fruits=>(apples,mangoes),toys=>(guns,puzzles),computer=>(mouse,keyboard)). Here all our values have become lists. We can hardly imagine this one as stack or table. Here come the matrices to our rescue.
  I found a definition for3d matrix on internet. It said "Matrices can be thought of as representing the transformation (or change) in orientation and position required to get from one Coordinate Space, or Frame of Reference, to another one". I said "CRAP". It is true but misleading. First of all, we don't need transformation from one co-ordinate space to another one for a 3d Matrix setup, it can happen on 1 space only. Because space itself is 3d.
  Anyway, after clearing the mind from that mambo-jumbo by rinsing with fresh thoughts I thought why not define the matrix in an easy understandable language. Then this definition bubbled in my mind. Consider 3D Matrices as multiple stacks of objects placed side-by-side. This means that our indexed arrays (or lists as for the PERL people) come back and make our life easy. consider our example (fruits=>(apples,mangoes),toys=>(guns,puzzles),computer=>(mouse,keyboard)). Here consider 3 stacks, (apples,mangoes), (guns,puzzles), (mouse,keyboard). we can visualize these as stacks as discussed earlier. now place them side by side from left to right, then give each stack a name. This name is nothing but its corresponding key. So our stack (apples,mangoes) gets name fruits and so on. So here we have our multidimensional array, just understood well and ready for manipulation.
  Arrays are a concept conceived to make programming complicated logic easily. Yet, little knowledge is dangerous and so is with arrays in PHP. All arrays are meant to store information temporarily and be manipulated as and when required. In my next post I will look at some of the major functions provided by PHP to manipulate the arrays.

Thanks for reading. I hope you enjoyed reading like I did writing.
--
Amit Kriplani
PHP Web/Application Developer
Blog:http://blog.amitkriplani.com
Mobile: +91-9913932644
MSN: amitk87@hotmail.com
Gmail: amitkriplani5@gmail.com
Yahoo: amitkriplani@ymail.com
Skype: amitkriplani
Twitter: @amitk87