Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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)

share|improve this question
    
Can you describe what's wrong with your code? –  Machavity Jul 23 at 17:13
1  
Why not just include the first file instead of the parsing? –  ForguesR Jul 23 at 17:14
    
@ForguesR the first file is a text file, he wants to use some value from the test file to pick which array from the included other php file to use. –  serakfalcon Jul 23 at 17:14
    
@Machavity, i dont get anything from my when i call my variabl variable, it is suppose to change the name of the varaible EXP:${$NomVar)} into $array but it dosent seems to work. But if i hardcode this with $array i get the array that has that name in PHPPage2.php –  Quebec_Immortal Jul 23 at 17:19

1 Answer 1

up vote 0 down vote accepted

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]
share|improve this answer
    
I'll give a try :) thanks serak –  Quebec_Immortal Jul 23 at 17:25
    
$GLOBALS[${$NomVar}] Works like a charm! Thanks! –  Quebec_Immortal Jul 23 at 17:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.