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
$1
isn't a valid variable name. – Magnus Eriksson Jan 19 at 16:49extract()
only works with associative arrays. – Magnus Eriksson Jan 19 at 16:51echo $workingarray[0][1]
orecho $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$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