A multidimensional array is an array in which you can store other arrays.
It is very similar to an associative array.
However, the associates for this array are arrays themselves.
For a better understanding, let us start the program.
I will create a program that lets you see the position of a letter in the English alphabet.
For example, if I give the value 1, it should echo out "A" in position 1.
If I give the value two it would say "B" in position 2.
And for three, it will say "C" is in position 3, and so on.
First I will create my own array.
And just for easy viewing, I will bring this down.
You are quite welcome to do so yourself.
And inside. I will create my own array, which I will call ‘ABC’.
That will be the array.
Instead of putting a value here, as we did before, we have an array inside.
And inside these, will be the values, for example, Capital A, B, C and D.
And these values will be separated by commas.
And then we type “123” and that is equal to an array.
Now we’re just going to have ‘1,2,3,4, and that’s it.
Down here, I’ll show you how to echo out specific data inside the array.
We’ll call our main array.
And we’ll call this array as well.
And then the position of what you want inside the array. So it is an array inside an array.
So I will just type ‘echo’ and then ‘alpha’ which is our main array.
And then inside square brackets, ‘ABC’.
And next, inside square brackets, the position of the element if you want to retrieve.
Now, for example, is going to echo "A".
Let us give that a run - and we got "A".
Changing this to ‘123’, will hopefully give us "1".
As you can see here.
So we've made our two basic arrays inside our main arrays, and we’ve learnt to call it.
Now I’m going to create a new program to find out the position of a letter in relation to its number.
I’m going to type up here ‘ postion = 0’, since 0 is the beginning.
Now I will echo out ‘Letter something is in position something’.
This is going to be quite simple.
We enter a position here, say 3. Since C is in position 3 in the alphabet, we get C.
So , to echo out our letter, if I am going to replace the first blank with ‘alpha’.
ABC
'pos'
as 'pos' represents our position.
So then, the position will be - Alpha... 123
And then the position, ‘pos’.
At the moment, position is equal to 0.
We type ’echo something’. So this is position zero.
Position zero inside the internal array “ABC”. So in actual fact, we are saying that A is in position 0,
which is this array, 123 and that is position zero. So actually we are saying letter A is in position one.
Lets run this. Okay. A is in position 1. Let’s change this to 1.
Refresh. Letter B is in position 2. Now what I will do to make this application fully functional and easy to navigate, is eliminate the necessity to write zero for 1.
So I will put ‘-1’ at the end and put 1 in brackets for better legibility.
So, position one minus one is infact zero. So, writing 1 will give the same result as writing 0. Writing 2 will give us the same result as writing 1...letter B is in position 2.
If I put 1 then we get A is in position 1. So if I put zero here; there is no position -1; so we get “letter in position”. So we don’t have the letter or the position.
So, I’ve made that a bit more user-friendly. Thanks for watching!