Arrays - general chat

greg's picture

He has: 1,581 posts

Joined: Nov 2005

I've used arrays and session a lot recently
And have many times come across a situation where it would be ideal to have more than one value to a single key

It sometimes becomes difficult associating keys and values in an array with other keys and values. more often than not more than one key/value has data that is associated

The usual answer is putting them into the array in order, or ordering the array. That becomes difficult or mind blowing sometimes with large sites with user input, as user actions can't always be anticipated, or more often their actions can be anticipated but you can't do what you want with the array based on a certain potential user input.

I know you can have an array as the value of an array, but then you are stuck with associating a secondary array's values and keys with the primary array's values and keys where they are to be associated

I did think of a solution, but that requires writing new things into the php structure.

Is there something out there to counter this issue?. I certainly couldn't find one in PHP though.

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

Can you give a brief example of data you are wanting to store in the array?

-Greg

greg's picture

He has: 1,581 posts

Joined: Nov 2005

well there are a few instances this would be helpful
but a current project I have is a shopping cart
just a basic cart - adding/removing data from sessions

so say you have 20 items in the DB
and the user clicks various things on various pages
each item has multiple data and not just data from the db, the user also enters input, so it cannot simply be taken from the DB on the cart page via the array's key matching the DB row ID

so then you have
[item name]=> CD [price] => 12.00 [artist] => rolling stones [quantity] => 3

[quantity] is a user input
so when you come to display their cart (usually a table) the data is in order in the array, so a foreach or while in a limited loop
as above example would be 4 loops of then a new then the 4 loops of again for each instant in the array (each item)

but what if other items in the array have more than 4 keys per item
you either need to do an IF and check the key on the 5th and if not CD its something else and then loop for however many keys that item has
or use another array

where an array with one key and multiple values under that one key would be better
just do a foreach on that key then it moves onto the next. doesn't matter if there are 5 values, 7 values etc
it will simple echo all the values for that one key, keeping all data for each item together
still somehow have a reference to each value in that key, like a secondary key

this is just a shopping cart, but of course other curcumstances would benefit from this, especially where user input is required such as quantity coupled with different number of values/keys for different groups within the same array

having an array in an array helps. so array2 is a value in array1
but that isnt as simple as having multiple values per key
again, this isn't something we can just start using, it would have to be implemented into the php structure

it was just a thought really
I am not stuck with anything particularly, as I always find a way around stuff. Even if a little bodgy at times Laughing out loud

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.