How to Add a Dimension to an Array?

If I have a basic Indexed Array like this…

	0 => string 'BettyBoop' (length=9)
	1 => string 'Superman' (length=8)
	2 => string 'JamesDean' (length=9)
	3 => string 'KingKong' (length=8)

How can I add an extra dimension to it, so it looks like this…

	'BettyBoop' =>
		'sendType' => int 0
	'Superman' =>
		'sendType' => int 0
	'JamesDean' =>
		'sendType' => int 1
	'KingKong' =>
		'sendType' => int 1

Or maybe this is another way to show it in a more tabular format…

Key1	Value/Key2	Value2
-----	-----------	-------
0	BettyBoop	0
1	Superman	0
2	JamesDean	1
3	KingKong	1



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

Code to convert your existing array into your new format:

$newArr = array();
foreach($myArr as $string) {
     $newArr[$string] = 1;

Write it statically:

$myArr = array(
    'BettyBoop' => 1

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)

$myArr = array(
    'BettyBoop' => array(
        'foo' => 'bar'

Thanks for the responses.

I need to chew on things…

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

Hope you follow me somewhat…



This thought just occurred to me…

Before supper, I was going to merge $_POST[‘to’] and $_POST[‘bcc’] into one array, and make the Usernames the key, and the RecipientType the value.

And while that would work for most of my data validation routines, it would create an issue if some yahoo did this…

To: DoubleDee; DoubleDee; DoubleDee

Bcc: DoubleDee; DoubleDee

…because all of my Keys would collide.

And yet, I need to make sure there aren’t any dups across all “To:” and “Bcc:” entries…



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

This little block of code I wrote seems to be helping out…

	// Add To-List Values to All-List Array.

	foreach($toListArray as $toUsername){
		// To-Recipient is Not Blind.
		$allListArray[$x][$toUsername] = 0;

	// Determine where to start Index.
	$y = count($toListArray);

	// Add Bcc-List Values to All-List Array.
	foreach($bccListArray as $bccUsername){
		// Bcc-Recipient is Blind.
		$allListArray[$y][$bccUsername] = 1;

This yields an array like this…


  0 => string 'DoubleDee' (length=9)
  1 => string 'Superman' (length=8)


  0 => string 'username1' (length=9)
  1 => string 'usrname2' (length=8)
  2 => string 'username3' (length=9)


  0 =>
      'DoubleDee' => int 0
  1 =>
      'Superman' => int 0
  2 =>
      'username1' => int 1
  3 =>
      'usrname2' => int 1
  4 =>
      'username3' => int 1



Are you allowing the same name in the cc, bcc or preventing it?

My existing code (i.e. before adding the “Bcc:” field) does the following checks…

  • Check for Recipients in “To:” field

If found…

[INDENT]- Build To-List Array

  • Check Recipient Count (Must be <= 5)
  • Check for Recipient Dups
  • Check Recipient Format (RegEx)
  • Check for Recipient in Database

Why do you ask?


If you are not allowing a user to be in a cc / bcc more than once, then your code at its simplest form would be this:

$to = array();
$cc = array();
foreach(explode(";", $_POST['to']) as $user) {
    if(!in_array($user, $to)) array_push($user, $to);

foreach(explode(";", $_POST['cc']) as $user) {
    if(!in_array($user, $to) && !in_array($user, $cc)) array_push($user, $cc);