在php中使用变量的值作为数组名?

时间:2015-03-10 04:10:47

标签: php arrays foreach

晚上全部。 我有一个可能的get变量数组,我用它来包含外部数组。 (aArray.php包含一个名为a的数组)

$options = array ("a","b","c");
$all=array();

foreach($options as $getVariable) 
    {
    if (isset($_GET[$getVariable])) {

        include($getVariable . "Array.php");
        array_push($all, ????); 
        }
    }

它的工作正常包括在内。我的问题是array_push。有没有办法渲染$ getVariable的值并将其用作变量? (例如,在包含$ a之后,我想将其添加到$ all)

1 个答案:

答案 0 :(得分:0)

您可以将包含的文件看起来像这样。

<?php

$myVars = ['foo' => 'bar'];

然后使用array_push($all, $myVars);

或者

如果您收录的文件看起来像这样。

<?php

return ['foo' => 'bar'];

然后你可以使用array_push($all, include($getVariable . "Array.php"));

无论哪种方式,$all都会== [['foo' => 'bar']]