0

So I have a List of Variables

$TestData1="Hello";
$TestData2="";
$TestData3="0";
$TestData4="Yes";
$TestData5=" ";
$TestData6="No";

I want to make a function that will run all these variables through a filter. I want to make this a loop that checks all the variables in one shot. I had the idea of storing the variable names in an array. This is shown below.

$TestArray=array("TestData1", "TestData2", "TestData3", "TestData4","TestData5","TestData6");

So my main question is how would I take these names in the array and run a loop that checks to see if a certain condition is met. Example below.

foreach ($TestArray as $Data):

   $VariableToTestConnditions="$".$Data;



endforeach;

I know that statement doesn't work, but it is all I could think of. The out come of this would be if the variable value =="Yes" then is would change the original variable's value to "N/A". So after it checks all the variables, it would change $TestData4 to "N/A".

6
  • Why not store data in an array? Commented Aug 23, 2013 at 2:16
  • if you need to keep the name of the variable (eg TestData1), just use an associative array: $TestArray = array("TestData1" => "Hello", "TestData2" => "", ....); Commented Aug 23, 2013 at 2:17
  • @zerkms I need to change the original variable value. Commented Aug 23, 2013 at 2:17
  • @kennypu That does not allow me to change the original value of the variable. Thank you for the suggestion. How would I use the Name of the value as a Variable? Commented Aug 23, 2013 at 2:18
  • @KevinWeber yes it does. the question is: do you need to have all those separate variables? if not, you can just use an associative array and keep track of them there. I'll post an answer using it. Commented Aug 23, 2013 at 2:19

3 Answers 3

6

instead of having an array of the names, it would make more sense to have an associative array (key value pairs):

$TestArray = array(
    "TestData1" => "Hello",
    "TestData2" => "",
    "TestData3" => "0",
    "TestData4" => "Yes",
    "TestData5" => " ",
    "TestData6" => "No"
);

now if you wanted to test a variable:

foreach($TestArray as $key => $value) {
    if($VariableToTestConnditions == $value) {
        //do something 
    }
}

if you wanted to change the value of a testdata:

$TestArray["TestData1"] = "Good Bye";

this way is much neater and simpler.

Sign up to request clarification or add additional context in comments.

Comments

2

i used echo to demo the syntax, you can use what you like

$TestData1="Hello";
$TestData2="";
$TestData3="0";
$TestData4="Yes";
$TestData5=" ";
$TestData6="No";

$TestArray=array("TestData1", "TestData2", "TestData3", "TestData4","TestData5","TestData6");


foreach($TestArray as $a){

echo ${$a};
 //or 
echo $$a;

}

Comments

2

Use the two-dollar sign.

See PHP variable variables: http://php.net/manual/en/language.variables.variable.php

foreach ($TestArray as $Data):

   $VariableToTestConnditions=$$Data;

endforeach;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.