Well, the item cannot be both a key and a value at the same time - without duplication, anyway. So you’re not just adding a dimension, you’re changing the index of the previous dimension (from integers to strings)
Do you -need- to maintain “key1”? or can the base array use the names as keys?
If the latter, array_flip your array (which will turn your values into keys, and keys into values), and then assign the values to the appropriate keys. (Or, assuming you actually create the initial array, simply construct it with the additional dimension in the first place.)
In a key value setup, your value can also be an array (creating a multi dimenionsal array) and those child arrays can either be named as well. (All arrays are named really, when you dont specify a key, it will auto increment the key)
In the mean time, allow me to explain a bit more of what I am trying to do!
I just re-wrote my “send-pm.php” script to allow the Sender to PM multiple Recipients at once. (No small feat, counter to what others think!)
It is done and works great, but now I want to allow the Sender to not only PM multiple people, but choose whether a Recipient is a “To:” or a “Bcc:”
(This is actually a rather involved new project for me!!)
I sorta need two things all at once…
On one hand, it would be easier to merge $_POST[‘to’] and $_POST[‘bcc’] together in one array so I can do things like check whether a username is in the database using one array.
Then again, if I just merged things together using my existing code, I would have no way to know who is a “To” and who is a “Bcc”?! :-/
I am thinking that just “adding another dimension to my two-dimensional” array would be the smartest way to approach things. (With records, if you need to manipulate 4 things, you just query up 4 fields. So I guess I need to think along those lines with arrays…)
Lemme look at my existing code (i.e. “To” only) and at your suggestions, and see if I can start to figure out this latest puzzle?!
If you do not need to store anything other thanthe recipient, then look into in_array(), and store as array(‘address1’, ‘address2’), using in_array() before adding the address again. If you need to store other attributes, use the address as the key and an array under it to store attributes such as array(‘address’ => array(‘foo’ => ‘bar’))