PHP - 如何从另一个带有变量变量的php文件中获取变量

时间:2014-07-23 17:10:59

标签: php arrays variables variable-variables

我遇到了一个非常棘手的问题......我正在制作一个程序,它将从另一个php文件中获取数组,将对其进行排序并使用新数组覆盖php文件。我正在对2个php文件进行一些测试。一个是index.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"
 );
?>

这是 index.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,但这不起作用:(

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

1 个答案:

答案 0 :(得分:0)

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

作弊
$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]