Go through an array with a FOR loop

0

I'm trying to repeat N times an array with a FOR loop, but I've been stuck in the last array that contains more elements. This is my code so far.

$datos = array();
for ($i = 1 ; $i <= 50 ; $i++) {
    $medidor = 'medidor'.mt_rand(1,99);
    for ($j = 1 ; $j <= 50 ; $j++) {
        for ($k = 0 ; $k < 5 ; $k++) { 
            $datos[$medidor.$i][$j] = array('fecha' => fecha_aleatoria('d/m/Y H:i:s'), 'valor' => mt_rand(1,200));
        }  
    }
}

for ($i = 0 ; $i < 50 ; $i++) { 
    $centro->insertOne(['nombre' => 'CENTRO-'.mt_rand(1,99), 'datos' => $datos]);
}

I need to repeat 5 times the values of the array that contains the date and a value.

array('fecha' => fecha_aleatoria('d/m/Y H:i:s'), 'valor' => mt_rand(1,200));

Something like:

Fecha: 05/03/2015
Valor: 25
Fecha: 10/12/2012
Valor: 45
Fecha: 15/06/2005
Valor: 67
Fecha: 26/05/2009
Valor: 78
.
.
.

I've tried it in many ways, but I can not. Someone to give me a hand how to repeat that array N times?

EDIT: For a better understanding of my question, this is what I get in Robo3T (DB NoSQL Visual Manager, in this case mongodb)

    
asked by A. Morales 12.03.2018 в 16:25
source

2 answers

2

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);  
    }
}
    
answered by 12.03.2018 в 17:37
0

Your problem is that you are using three-dimensional arrays unnecessarily, since you could put together a two-dimensional array with key = > value, BUT I will answer your question in particular. Maybe the ones you're looking for is a foreach

foreach (expresión_array as $valor)
        sentencias 
foreach (expresión_array as $clave => $valor)
        sentencias

If you do:

foreach ($datos as $dato) {
//Es aquí donde necesitas el doble loop, recuerda que estás usando un array tridimencional donde cada clave es un entero 
    foreach ($dato as $valor){

        var_dump($valor['fecha']);
        var_dump($valor['valor']);
    }
}

// output

string(10) "12/03/2018"
int(199)
string(10) "12/03/2018"
int(122)
string(10) "12/03/2018"
int(186)
string(10) "12/03/2018"
int(23)
string(10) "12/03/2018"
int(75)
string(10) "12/03/2018"
int(147)
string(10) "12/03/2018"
int(130)

Which is definitely what you wanted.

    
answered by 12.03.2018 в 17:47