PHP - 如何使用变量变量从另一个 php 文件中获取变量

PHP - How to get a variable from another php file with a variable variable

提问人:Fearware 提问时间:7/24/2014 最后编辑:Fearware 更新时间:7/24/2014 访问量:91

问:

我遇到了一个非常棘手的问题......我正在制作一个程序,它将从另一个 php 文件中获取数组,对其进行排序并用新数组覆盖 php 文件。我正在对 2 个 php 文件进行一些测试。一个是索引 .php 另一个是 PHPPage2.php ( 这是带有数组的那个 )。

代码如下:PHPPage2.php

<?php 
 $array = array(
 "d" => "lemon", 
 "a" => "orange", 
 "b" => "banana", 
 "c" => "apple"
 );

 $fruits = array(
 "d" => "lemon", 
 "a" => "orange", 
 "b" => "banana", 
 "c" => "apple"
 );
?>

这是索引.php

$NomArray = array();
            $lines = array(); #That's an array of lines, it gets all line of the file
            $fp = fopen("Test/TextFile.txt", "r");
            while (!feof($fp))
            {
                $line = fgets($fp);               

                #Lookinf for an "array" variable
                for($i = 0; $i < strlen($line); $i++)
                {
                    if($line[$i] == 'a' and $line[$i-1] <> '$' and $line[$i+1] == 'r' and                $line[$i+2] == 'r' and $line[$i+3] == 'a' and $line[$i+4] == 'y')
                    {
                        $line = trim($line);

                        $lines[]=$line; 
                        $NomVariable = "";

                        #looking for the name fo the variable
                        for($i = 0; $i < strlen($line); $i++)
                        {
                            # if there is a $, it's a variable
                            if($line[$i] == '$')
                            {
                                # we take all char until that we hit a "=" or " "
                                for($j = $i; $j < strlen($line); $j++)
                                {
                                    if($line[$j] <> '=' and $line[$j] <> ' ')
                                    {
                                        $NomVariable = $NomVariable . $line[$j];
                                    }
                                    else
                                    {
                                        break;
                                    }   
                                }
                            }
                        }
                        #We put the name of the variable in a array 
                        $NomArray[] = $NomVariable;
                    }                  
                } 
            }
            fclose($fp);

    #We include PHPPage2.php
    include "Test/PHPPage2.php"; 

    # a for loop to get all variable name HERE IS MY PROBLEM
    for($i = 0; $i < count($NomArray); $i++)
    { 
       # I wanna use what is inside $NomArray[$i] as the name of a dynamic variable
       $NomVar = $NomArray[$i];

       /*

       I want to sort the array that as the name of $NomArray[$i],
       But I dont know why, when i = 0 ${$NomVar)} is suppose to be equal to $array...
       That seems logic to me, but it dosent work...

       */
       asort(${$NomVar});
       foreach (${$NomVar} as $key => $val) 
       {
         echo "\"$key\" => \"$val\", \n";
       } 

    }

我也看过 http://php.net//manual/en/language.variables.variable.php 但那不起作用:(

提前感谢您的帮助! (对不起,如果我的英语不是最好的)

php 数组 变量

评论

0赞 Machavity 7/24/2014
你能描述一下你的代码有什么问题吗?
1赞 ForguesR 7/24/2014
为什么不只使用第一个文件而不是解析呢?include
0赞 serakfalcon 7/24/2014
@ForguesR第一个文件是文本文件,他想使用测试文件中的一些值从包含的其他 php 文件中选择要使用的数组。
0赞 Fearware 7/24/2014
@Machavity,当我调用我的变量时,我没有从我的变量中得到任何东西,它应该将可变 EXP:${$NomVar)} 的名称更改为 $array但它似乎有效。但是,如果我用 $array 对其进行硬编码,我就会得到在 PHPPage2 中具有该名称的数组.php

答:

0赞 serakfalcon 7/24/2014 #1

由于您已将这两个数组声明为全局变量,因此可以使用

$GLOBALS[$NomVar]

这将为您提供您正在寻找的数组。但是,更明智的解决方案是将两个阵列嵌套在另一个阵列中

<?php 
$choices = array('array'=> array(
                  "d" => "lemon", 
                  "a" => "orange", 
                  "b" => "banana", 
                  "c" => "apple"
                ),'fruits' => array(
                  "d" => "lemon", 
                  "a" => "orange", 
                  "b" => "banana", 
                  "c" => "apple"
                ));
?>

因此,您可以使用数组的键来检索数组

$choices[$NomVar]

评论

0赞 Fearware 7/24/2014
$GLOBALS[${$NomVar}] 就像一个魅力!谢谢!