Search code examples
phparraysvariablesvariable-variables

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


I got a pretty tricky problem... I'm making a programm that will get the array from another php file, will sort it and will overwrite the php file with the new array. I'm making some test on 2 php files. One is index.php and the other one is PHPPage2.php ( thats the one with the arrays ).

Here is the code : PHPPage2.php

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

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

And here is 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";
       } 

    }

i've also looked at http://php.net//manual/en/language.variables.variable.php but that dosen't work :(

Thanks in advance for your help! (Sorry if my english isn't the best)


Solution

  • since you've declared the two arrays as globals, you can cheat by using

    $GLOBALS[$NomVar]
    

    which will give you the array you are looking for. However, a smarter solution would be to nest both arrays inside another array

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

    so you could retrieve the arrays by using their keys

    $choices[$NomVar]