在数组中存储变量名称

时间:2021-04-13 23:14:03

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

我知道这句话不起作用,但这是我能想到的。如果变量值==“是”那么将是原始变量的值改为“N / A”。因此,在检查所有变量之后,它会将$ TestData4更改为“N / A”。

3 个解决方案

#1


2  

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

我使用echo来演示语法,你可以使用你喜欢的

$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;

}

#2


5  

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:

如果你想改变testdata的值:

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

this way is much neater and simpler.

这种方式更整洁,更简单。

#3


2  

Use the two-dollar sign.

使用两美元符号。

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

请参阅PHP变量变量:http://php.net/manual/en/language.variables.variable.php

foreach ($TestArray as $Data):

   $VariableToTestConnditions=$$Data;

endforeach;

#1


2  

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

我使用echo来演示语法,你可以使用你喜欢的

$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;

}

#2


5  

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:

如果你想改变testdata的值:

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

this way is much neater and simpler.

这种方式更整洁,更简单。

#3


2  

Use the two-dollar sign.

使用两美元符号。

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

请参阅PHP变量变量:http://php.net/manual/en/language.variables.variable.php

foreach ($TestArray as $Data):

   $VariableToTestConnditions=$$Data;

endforeach;