I'm trying to use dynamic variable names (I'm not sure what they're actually called) But pretty much like this:
for($i=0; $i<=2; $i++) {
$("file" . $i) = file($filelist[$i]);
}
var_dump($file0);
The return is null
which tells me it's not working. I have no idea what the syntax or the technique I'm looking for is here, which makes it hard to research. $filelist
is defined earlier on.
i have a solution for dynamically created variable value and combined all value in a variable.
Wrap them in
{}
:Working Example
Using
${}
is a way to create dynamic variables, simple example:I do this quite often on results returned from a query..
e.g.
Now I can just use $MyFieldname (which is easier in echo statements etc) rather than $MyQueryResult['MyFieldname']
Yep, it's probably lazy, but I've never had any problems.
I was in a position where I had 6 identical arrays and I needed to pick the right one depending on another variable and then assign values to it. In the case shown here $comp_cat was 'a' so I needed to pick my 'a' array ( I also of course had 'b' to 'f' arrays)
Note that the values for the position of the variable in the array go after the closing brace.
Try using
{}
instead of()
:Tom if you have existing array you can convert that array to object and use it like this: