SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,764
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    How to Add a Dimension to an Array?

    If I have a basic Indexed Array like this...
    Code:
    array
    	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...
    Code:
    array
    	'BettyBoop' => 
    		array
    		'sendType' => int 0
    	'Superman' => 
    		array
    		'sendType' => int 0
    	'JamesDean' => 
    		array
    		'sendType' => int 1
    	'KingKong' => 
    		array
    		'sendType' => int 1

    Or maybe this is another way to show it in a more tabular format...
    Code:
    Key1	Value/Key2	Value2
    -----	-----------	-------
    0	BettyBoop	0
    1	Superman	0
    2	JamesDean	1
    3	KingKong	1
    Sincerely,


    Debbie

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    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.)
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  3. #3
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,181
    Mentioned
    65 Post(s)
    Tagged
    2 Thread(s)
    Code to convert your existing array into your new format:
    Code PHP:
    $newArr = array();
    foreach($myArr as $string) {
         $newArr[$string] = 1;
    }

    Write it statically:
    Code PHP:
    $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)
    Code PHP:
    $myArr = array(
        'BettyBoop' => array(
            'foo' => 'bar'
        )
    );

  4. #4
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,764
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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...

    Sincerely,


    Debbie

  5. #5
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,764
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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...
    Code:
    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...

    Sincerely,


    Debbie

  6. #6
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,181
    Mentioned
    65 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by DoubleDee View Post
    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...
    Code:
    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...

    Sincerely,


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

  7. #7
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,764
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    This little block of code I wrote seems to be helping out...
    PHP Code:
        // Add To-List Values to All-List Array.
        
    $x=0;

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

        
    // 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;
            
    $y++;
        } 

    This yields an array like this...


    toListArray
    Code:
    array
      0 => string 'DoubleDee' (length=9)
      1 => string 'Superman' (length=8)

    bccListArray
    Code:
    array
      0 => string 'username1' (length=9)
      1 => string 'usrname2' (length=8)
      2 => string 'username3' (length=9)

    allListArray
    Code:
    array
      0 => 
        array
          'DoubleDee' => int 0
      1 => 
        array
          'Superman' => int 0
      2 => 
        array
          'username1' => int 1
      3 => 
        array
          'usrname2' => int 1
      4 => 
        array
          'username3' => int 1
    Sincerely,



    Debbie

  8. #8
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,181
    Mentioned
    65 Post(s)
    Tagged
    2 Thread(s)
    Are you allowing the same name in the cc, bcc or preventing it?

  9. #9
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,764
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by K. Wolfe View Post
    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...
    - 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?


    Debbie

  10. #10
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,181
    Mentioned
    65 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by DoubleDee View Post
    My existing code (i.e. before adding the "Bcc:" field) does the following checks...

    - Check for Recipients in "To:" field

    If found...
    - 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?


    Debbie
    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:

    Code PHP:
    $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);
    }


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •