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.

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?! :-/

Am I making any sense?

Debbie

oddz
—
2012-05-31T03:09:40Z —
#5

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.

DoubleDee
—
2012-05-31T04:56:07Z —
#6

oddz said:

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.

But what about my long response above your last post????

Debbie

ITSE1306
—
2012-05-31T18:27:51Z —
#7

Hi DoubleDee,

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.

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(...) ).

ParkinT
—
2012-05-31T18:36:03Z —
#8

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.

ServerStorm
—
2012-05-31T18:39:51Z —
#9

ParkinT said:

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.

Yes very impressive post. I too hope would like to see you more often! Welcome!

ITSE1306
—
2012-05-31T19:47:06Z —
#10

ServerStorm said:

Yes very impressive post. I too hope would like to see you more often! Welcome!

Thanks! I was once where DoubleDee was/is. And it is a really good question. One I've had before in my early days, but could never articulate.

DoubleDee
—
2012-05-31T21:21:42Z —
#11

ITSE1306,

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...

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...

Thanks,

Debbie

ParkinT
—
2012-06-01T11:42:56Z —
#12

DoubleDee said:

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.

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?

DoubleDee
—
2012-06-01T17:40:57Z —
#13

ParkinT said:

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?

Yeah. Why no one is seeing things as I mentioned above?! :lol:

No one has "articulated" things from a Normalized Database standpoint, and I am curious why.

Wouldn't my approach also work?

Debbie

DaveMaxwell
—
2012-06-01T19:04:02Z —
#14

DoubleDee said:

No one has "articulated" things from a Normalized Database standpoint, and I am curious why.

Wouldn't my approach also work?

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.

ServerStorm
—
2012-06-01T20:21:01Z —
#15

DaveMaxwell said:

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.

Hi Debbie,

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.

Steve

DoubleDee
—
2012-06-02T00:07:20Z —
#16

ServerStorm said:

Hi Debbie,

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.

Steve

I'm just trying to better understand how arrays work and how to build multi-dimensional ones.

The examples I gave were purely academic. I have no need - or honestly desire - to work with multi-dimensional arrays at this point.

I just figured building one with some data that I can easily relate to would be a good exercise.

Since I have figured out my recursive function independent of this conversation, maybe this topic no longer matter?

Thanks,

Debbie

system
—
2012-06-02T05:57:06Z —
#17

DoubleDee said:

I would still like to know what people think about the way I did things in Post #4...

`

KEY VALUE0 Led Zeppelin1 Led Zeppelin2 Cream

`

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:

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)))

- 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?

You have three tables: - one for group: ( group id, group name ), - one for album: ( foreign group id key, album id, album name ) - one for song: ( foreign group id key, foreign album id key, song id, song name )

What is different in databases from associative arrays is that you'd have unique id's for album and song too:

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)))

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.