You can not do this, at least not directly. And even with the gambiarra 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 two-dimensional array .
$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 are doing there is putting arrays into another array , so it has 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 gambiarras. I thought of the two I read here. Please do not use a complicated solution that can cause undesirable effects.
Variable variables are not true, complete variables, they can not be used in any situation. They subvert what is expected of normal code, they make it difficult to maintain.
associative arrays are at least makes explicit that you are not using variables but rather indexes to simulate the variable. But it's 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 simpler, easier, more accurate and elegant solution. So you do not have to use it.
There is still room for improvement and making it all a variable. You have three possible solutions:
Create another dimension to get date
, time
and valeur
as numeric indexes. It is an optimization attempt but that does not 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 element. This reverses the logic used a bit 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.
Create a new dimension as associative array . Probably the preferred one because it is less ceremonial 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();
}