ID GROUP ALBUM SONG
1 Led Zeppelin Led Zeppelin III Friends
Gallows Pole
That's the Way
1 Led Zeppelin Houses of the Holy Over the Hills and Far Away
D'yer Mak'er
The Ocean
2 Cream Disraeli Gears Strange Brew
SWLABR
However, if this was returned by a database you would not get this structure and then this structure is one array per row. The multiple values in the songs column would be a bad thing if this was the case.
A database would likely return a result set like:
[TABLE=“class: grid, width: 500”]
[TR]
[TD]ID[/TD]
[TD] GROUP [/TD]
[TD] ALBUM[/TD]
[TD]SONG[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]Led Zeppelin[/TD]
[TD] Led Zeppelin III [/TD]
[TD]Friends [/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]Led Zeppelin[/TD]
[TD] Led Zeppelin III [/TD]
[TD]Gallows Pole [/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]Led Zeppelin[/TD]
[TD] Led Zeppelin III [/TD]
[TD] That’s the Way[/TD]
[/TR]
[/TABLE]
This would probable be better served with 4 tables Groups, Albums, Songs, and Groups2Albums2Songs (as g2a2s). Then the following rows in the g2a2s table might be;
'GROUP' => 'Led Zeppelin'
'ALBUM' => 'Led Zeppelin III'
'SONGS' => array("Friends", "Gallows Pole", "That's the Way")
'GROUP' => 'Led Zeppelin'
'ALBUM' => 'Houses of the Holy'
'SONGS' => array("Over the Hills and Far Away", "D'yer Mak'er", "The Ocean")
'GROUP' => 'Cream'
'ALBUM' => 'Disraeli Gears'
'SONGS' => array("Strange Brew", "SWLABR")
How can you have a key like ‘GROUP’ repeat 3 times?! :-/
I would have went with something like this for starters…
KEY VALUE
0 Led Zeppelin
1 Led Zeppelin
2 Cream
Or maybe something like this…
array(
Led Zeppelin=>array(
Led Zeppelin III=>array(
Friends=>3:55
Gallows Pole=>4:58
That's the Way=>5:38)));
array(
Led Zeppelin=>array(
Houses of the Holy=>array(
Over the Hills and Far Away=>4:50
D'yer Mak'er=>4:23
The Ocean=>4:31)))
array(
Cream=>array(
Disraeli Gears=>(
Strange Brew=>2:46
SWLABR=>2:32)))
I guess I just think in database terms and am having a hrd time visualizing and understanding things in array terms…
Furthermore, like a lot of things in programming, there seem to be a couple of different ways to do things, and so I never know which is the more correct way?! :-/
The most generic way to explain 90% of cases is that each row is a separate, associative array. Each key of the associative array is the table column name and value the column value.
To answer your first question – you technically have 1 array, which can be described as a multi-dimensional array.
A better visual representation of your array is below. As it is an array of array’s.
To answer your second question about having ‘GROUP’ repeat 3 times – each ‘GROUP’ is an associative key that is kept in a separate array. It seems to repeat, but it does not within each individual ordinal (aka record or index). If you were to repeat the ‘GROUP’ within the same ordinal of the array it would overwrite what was there before.
Array
(
[0] => Array
(
[ID] => 1
[GROUP] => Led Zeppelin
[ALBUM] => Led Zeppelin III
[SONGS] => Array
(
[0] => Friends
[1] => Gallows Pole
[2] => That's the Way
)
)
[1] => Array
(
[ID] => 1
[GROUP] => Led Zeppelin
[ALBUM] => Houses of the Holy
[SONGS] => Array
(
[0] => Over the Hills and Far Away
[1] => D'yer Mak'er
[2] => The Ocean
)
)
[2] => Array
(
[ID] => 2
[GROUP] => Cream
[ALBUM] => Disraeli Gears
[SONGS] => Array
(
[0] => Strange Brew
[1] => SWLABR
)
)
)
The neat thing about array’s is that you can create them in different ways. Below is the same array written differently for the PHP code.
And the same array again, written differently (usually used when you want to store information from a database into an array). It is done this way to break it down into simple steps for loops.
Of course, you could break up your original list into lots of separate arrays, but they would no longer have any direct relationship with one another.
The arrays above can be written differently. For example, I explicitly indicate an index number to your example (e.g. $music[1]). I can leave out the index number and let PHP number them for me automatically (e.g. $music = array(…) ).
Welcome @ITSE1306 to Sitepoint.
Your explanation was outstanding and very clearly presented.
What is more impressive is that this was your first post. Your willingness to participate - actively - and be so very helpful is laudable.
I hope we see you around here quite often; offering your experience and advice.
Thanks for the response. (I agree with the others you got off to a great start for your first-ever Post!! Welcome!!0
Your example was easy to follow and will be a good reference, however I would still like to know what people think about the way I did things in Post #4…
array(
Led Zeppelin=>array(
Led Zeppelin III=>array(
Friends=>3:55
Gallows Pole=>4:58
That's the Way=>5:38)));
array(
Led Zeppelin=>array(
Houses of the Holy=>array(
Over the Hills and Far Away=>4:50
D'yer Mak'er=>4:23
The Ocean=>4:31)))
array(
Cream=>array(
Disraeli Gears=>(
Strange Brew=>2:46
SWLABR=>2:32)))
I follow the way you did things, but to be honest feel like my example above more accurately reflects how things exist in the real world…
Thinking in Database terms…
The are many GROUPS
One GROUP can have zero or more ALBUMS
One ALBUM has one or more SONGS
Each SONG has attributes describing it (e.g. “Length”)
My Nested Array above shows the One to Many to Many relationship - that exists in real life - between GROUPS and ALBUMS and SONGS, whereas your example shows things denormalized and treats a GROUP and an ALBUM at the same level.
This isn’t necessarily wrong, but I think the way I did my array would be closer to 3rd Normal Form in a Database, right?
Of course, your way might be easier to work with in PHP?!
BTW, the whole purpose of this thread was just to better understand how to create Multi-Dimensional Arrays and what they look like in practice, so that I could create some and use them to test a recursive PHP Function that I was working on which is supposed to process any level of Multi-Dimensional Arrays…
I think you answered your own question. In database terms you would set up multiple arrays that are “relational” to each other.
Is there another question on your mind? That you have not quite articulated?
The answer to one would probably answer the other. The why is because you’re essentially trying to use an array to duplicate functionality that’s already available through database means. Just from a purely maintenance standpoint, a database is easier to maintain the data than trying to maintain the arrays. Since database recordset access is essentially glorified array access, I don’t see a benefit to trying to accomplish the same thing but have to go through the hassle of loading the array elements yourself.
But that’s just me - use the right tool for the job.
I agree with Dave. I do understand that you are trying to wrap your head around Arrays and you mentioned you have a function that can parse an non or multi-dimensional arrays, But at the end of the day this data should be stored in a database with proper relational integrity, which will make it easier and more efficient than parsing it in PHP.
This is wrong. You have two different keys (0 and 1) assigned to a unique value (Led Zeppelin). For it to be true, you should have a combined unique value, like for composed unique indexes:
KEY VALUE
0 (Led Zeppelin, Led Zeppelin III)
1 (Led Zeppelin, Houses of the Holy)
2 (Cream, Disraeli Gears)
Using the duplicate values for (key, band name), (key, album title) or (key, song title) instead of using dumb keys like ‘1’, ‘2’ etcetera, saves you the search for the keys of the values.
I guess this is what you were looking for: a little recognition for your idea and a little help to make it work. It’s not a new idea, but it’s good you’ve thought about it, because databases and arrays don’t necessarily translate one to another. Classic relational databases have stricter rules, but with arrays we can be more flexible.
PS BTW, band entries in $bands are on their way to look like objects.
Not necessarily true, but, just for fun:
a band = 1 object (definitely)
a band album = 1 object as a property for the band object (maybe)
a song = 1 object as a property for the album object property of the band object (unlikely)
What you’re missing are some methods to help define the behavior of the band.