You can’t do that, at least not directly. And even with the gambit you can do, it will produce one of the most bizarre codes I’ve ever seen.
If you call all variables of the same name you should use a array bidimmensional.
$newarray_date = array();
$newarray_time = array();
$newarray_valeur = array();
for($y = 0; $y <= 1; $y++){
$newarray_date[$y] = array();
$newarray_time[$y] = array();
$newarray_valeur[$y] = array();
}
What you’re doing there is putting arrays inside in one another array, Porting it simulates having two dimensions (PHP does not have the actual concept of array multidimensional).
Whenever you want a variable name to vary the solution is to create an index for this name and the index is possible through arrays.
Ideas are coming up that use skulls. I thought of the two I’ve read here. Please do not use a complicated solution that can generate undesirable effects.
Variable variables are not true variables, complete, they cannot be used in any situation. They subvert what is expected of normal code, they hinder maintenance.
Arrays associative are at least makes explicit that you are not using variables but rather indices to simulate the variable. But it gets a weird code.
I would have no problem using any of these solutions if the problem really required one of them. But there is a more simple, easy, correct and elegant solution. So there is no reason not to use it.
You can still improve even more and make all this one variable. There are three possible solutions:
Create another dimension to bring date
, time
and valeur
as numerical indexes. It’s an attempt at optimization but it doesn’t help much in PHP. Not helping to identify each element well, the code would be using magic numbers.
Create a class with date
, time
and valeur
as members of it and use instances of this class as an element. This somewhat reverses the logic used but it seems to me that it makes more sense since I understand that these three values are inseparable. Data structures should be preferred in these cases to give more semantics to the code.
Creates a new dimension like array associative. Probably preferred because it is less ceremonious than creating a class just for this while maintaining a good semantics. It would look something like this:
$newarray = array();
for($y = 0; $y <= 1; $y++){
$newarray[$y]['date'] = array();
$newarray[$y]['time'] = array();
$newarray[$y]['valeur'] = array();
}
I put in the Github for future reference.
I still don’t understand what you want old.... tries to give some examples to see if the question gets more
entendivel
– MarceloBoni
I don’t think you can do what you want. What akm wants is to create variables (arrays) with different names in a cycle. For example, there when
y==0
creates the array with the name:$newarray_date0
and wheny==1
creates the array with the name$newarray_date1
– Jorge B.
So@akm, try to give an example of what you want to do, maybe there’s another plausible solution
– MarceloBoni
What you want is to generate
arrays
dynamically?– MeuChapeu
Guy I think enters another array there in history
array("date0"=>array()...)
– Edi. Gomes