You have to take into account that you are generating 50 'meters' (with the $ i loop)
Each of those 50 'meters' have 50 data (generated by the $ j loop),
those 'data' are those that contain the date and the value. The $ k loop is over, since it overwrites the same value of a data 5 times.
With the last FOR loop you are creating 50 centers but you are placing the same data in all of them, despite this your information remains as follows:
//50 centros
centroN1 [//50 medidores por cada centro
medidorN1 [//50 datos por cada medidor
datosN1{fecha,valor},
datosN2{fecha,valor},
...
datosN50{fecha,valor}
],
medidorN2 [//50 datos por cada medidor
datosN1{fecha,valor},
datosN2{fecha,valor},
...
datosN50{fecha,valor}
],
...
medidorN50 [...]
]
centroN2 [//50 medidores por cada centro
medidorN1 [...],//50 datos por cada medidor
medidorN2 [...],
...
medidorN50 [...]
]
...
centroN50 [...]
So that each center has different information, what you can do is place the first loops in a function, and eliminating the variable $ k that does not do anything relevant, it would look like this:
function datos_centro() {
$datos = array();
for ($i = 1 ; $i <= 50 ; $i++) {
$medidor = 'medidor'.mt_rand(1,99);
for ($j = 1 ; $j <= 50 ; $j++) {
$datos[$medidor.$i][$j] = array('fecha' => fecha_aleatoria('d/m/Y H:i:s'), 'valor' => mt_rand(1,200));
}
}
return $datos;
}
for ($i = 0 ; $i < 2 ; $i++) {
$centro->insertOne(['nombre' => 'CENTRO-'.mt_rand(1,99), 'datos' => $datos]);
}
Your code in general features would be working and saving correctly, for the sample of that data you could use a structure similar to the FOR or in case you have N data you could use foreach within each 'center':
foreach ($centro->datos as $datos_medidor) {
echo 'Medidor: '.$i.'<br />'; //muestra el nombre del medidor
foreach ($datos_medidor as $fecha_valor) {
//muestra la fecha, pero con el formato 'd/m/Y H:i:s'
echo 'Fecha: '.$fecha_valor['fecha'].'<br />';
echo 'Valor: '.$fecha_valor['valor'].'<br />';
}
}
For the date to come out with the format you want ('d / m / Y') you can change the format at the moment you call insertOne () or at the moment of showing you can cut the string in the last loop using explode ()
foreach ($datos_medidor as $fecha_valor) {
$fecha = explode(' ', $fecha_valor['fecha'])[0];
echo 'Fecha: '.$fecha.'<br />';
echo 'Valor: '.$fecha_valor['valor'].'<br />';
}
You should detail more about your problem, to know if it is not registering the data as you wish or if the defect is in the sample of the data
EDIT
Apparently, you want to repeat the same values for the same magnitude . If that is the case, then what you have to do is change this:
for ($i = 1 ; $i <= 50 ; $i++) {
$medidor = 'medidor'.mt_rand(1,99);
for ($j = 1 ; $j <= 50 ; $j++) {
$datos[$medidor.$i][$j] = array('fecha' => fecha_aleatoria('d/m/Y H:i:s'), 'valor' => mt_rand(1,200));
}
}
Because of this:
for ($i = 1 ; $i <= 50 ; $i++) {
$medidor = 'medidor'.mt_rand(1,99);
$fecha = fecha_aleatoria('d/m/Y H:i:s');
$valor = mt_rand(1,200);
for ($j = 1 ; $j <= 50 ; $j++) {
$datos[$medidor.$i][$j] = array('fecha' => $fecha, 'valor' => $valor);
}
}