#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    150
    Rep Power
    3

    Question How to assign a value to a variable in an array?


    Hi all,

    The relavent code here for the question below.

    PHP Code:
    $POSTArray = array ($_POST['a'], $_POST['b'], $_POST['c'], $_POST['d'], $_POST['e'], $_POST['f'], $_POST['g'], $_POST['h'], $_POST['i']); //** Each may, or may not have a value **//
    $flagArray = array ($aFlag$bFlag$cFlag$dFlag$eFlag$fFlag$gFlag$hFlag$iFlag); //** All of these do not have a value yet **//

    $p count($POSTArray);

    for (
    $a=0;$a<$p;$a++) {
        if (
    $POSTArray[$a] != "") { 
        list(
    $someOtherReturnedVar$flag) = the_function $POSTArray[$a] );  //** Send info to the function. $flag is sent back as either "Y" or "N" **//
            
    $flagArray[$a] = $flag//** This is what isn't working, but is the "jist" of what I'm looking for **//
        

    }

    echo 
    "aFlag = ".$aFlag
    If $_POST['a'] contains a value, I want to set the resulting flag that comes out of the function to $aFlag (or is post b isn't null, then $bFlag should be set to $flag, etc. etc).

    However, when _POST['a'] does contain something, and then I echo out $aFlag, it's empty.

    How do I set the corresponding variable in the $flagArray to the $flag value?

    Thanks.
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,113
    Rep Power
    9398
    PHP Code:
    $flagArray[$a] = $flag
    Am I missing something?

    Ah, you want the $*flag variables to be updated too. Use references. As well as regular assignment.
    PHP Code:
    $flagArray = array (&$aFlag, &$bFlag, &$cFlag, &$dFlag, &$eFlag, &$fFlag, &$gFlag, &$hFlag, &$iFlag); 
    [edit] +1 to not using dozens of variables.
    Last edited by requinix; April 10th, 2013 at 05:24 PM.
  4. #3
  5. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    this is ... weird.

    Flooding the scope with dozens of variables is a bad idea, anyway. I don't know why people love that so much. When you got a bunch of related values, put them into an array! That's what they are for.

    Anyway, what exactly are you trying to do? What's your concrete code? I'm sure there's a sane way of solving the underlying problem.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    150
    Rep Power
    3

    Thumbs up


    Originally Posted by requinix
    [php]

    Ah, you want the $*flag variables to be updated too. Use references. As well as regular assignment.
    PHP Code:
    $flagArray = array (&$aFlag, &$bFlag, &$cFlag, &$dFlag, &$eFlag, &$fFlag, &$gFlag, &$hFlag, &$iFlag); 
    prepending the variables (in the array) with the "&" did it. that's exactly what I needed.

    thanks!!
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    150
    Rep Power
    3
    Originally Posted by Jacques1
    Hi,

    this is ... weird.

    Flooding the scope with dozens of variables is a bad idea, anyway. I don't know why people love that so much. When you got a bunch of related values, put them into an array! That's what they are for.

    Anyway, what exactly are you trying to do? What's your concrete code? I'm sure there's a sane way of solving the underlying problem.
    I needed to get the basic functionality fixed. Now that that's done, I'll work on making it more efficient.

IMN logo majestic logo threadwatch logo seochat tools logo