Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I want a value of my array to be displayed if I use a equel or almost equel variable. So for example if I have the following array line: [1] => g I want to display 'g' if I use the variable $1 (Or even better with the varible $arr1, so it does not interfere with other things later on.)

Here is my code: (I'm uploading a simple .txt file with some letters and making a array of each individual charachter):

$linearray = array();
$workingarray = array();

while(! feof($file)) {

        $line = fgets($file);

        $line = str_split(trim("$line"));
        $linearray = array_merge($linearray, $line);

}

$workingarray[] = $linearray;

print_r($workingarray);

When I have done this I will get this outcome;

Array ( [0] => Array ( [0] => g [1] => g [2] => h [3] => o [4] => n [5] => d [6] => x [7] => s [8] => v [9] => i [10] => s [11] => h [12] => f [13] => g [14] => f [15] => h [16] => m [17] => a [18] => g [19] => i [20] => e [21] => d [22] => h [23] => v [24] => b [25] => v [26] => m [27] => d [28] => o [29] => m [30] => v [31] => b [32] => ) )

I tried using the following to make it work:

extract($workingarray);
echo "$1";

But that sadly doesn't work. I just recieve this:

$1

And I want to recieve this:

g

It would be even better if I recieved the same effect with for example echo "$arr1" and then recieve g and for echo "$arr2" recieve h etc etc

share|improve this question
1  
I don't really get what you are trying to do but variables can't start with a number so $1 isn't a valid variable name. – Magnus Eriksson Jan 19 at 16:49
    
Stick with arrays. – AbraCadaver Jan 19 at 16:50
1  
Btw... extract() only works with associative arrays. – Magnus Eriksson Jan 19 at 16:51
1  
Why not just do: echo $workingarray[0][1] or echo $workingarray[0][2]? I don't see the benefit of having them as separate variables? If anything, it will get messier. – Magnus Eriksson Jan 19 at 16:55
1  
You could also remove the line $workingarray[] = $linearray; and use $linearray instead. Then you can skip the first [0] and just: echo $linearray[1]; – Magnus Eriksson Jan 19 at 17:05

This is simply impossible: http://php.net/manual/en/language.variables.basics.php

Variable names cannot start with a digit. The only allowable first char for variable names are letters and underscore.

And don't use extract or similar constructs. All they do is litter your variable namespace with unpredictable/unknown junk - you could very easily overwrite some OTHER critical variable with this useless junk, making for very difficult/impossible bugs to diagnose.

You're not saving any time by making up these new variables.

share|improve this answer
3  
I disagree that you should never use extract in your code. It can be very useful for templating and passing variables into views. In fact, Laravel uses exactly that when passing variables to Blade templates. It's true that it can be dangerous and hard to debug especially in this case, but saying it is a useless function is a bit overkill – samrap Jan 19 at 17:06

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.