Creating a different array related to different times a loop happens php


there. I'm having a problem with creating arrays in certain conditions in php, i'll try to explain. Here's my code:

for ($i = 1; $i < $tamanho_array_afundamento; $i++) {
    if ($array_afundamento[$i] - $array_afundamento[$i - 1] > 1) {
        $a = $array_afundamento[$i - 1];

        $con->query('CREATE TABLE IF NOT EXISTS afunda_$a
                     SELECT (L1_forma_tensao_max + L1_forma_tensao_min)/2 as L1_forma_tensao, (L2_forma_tensao_max + L2_forma_tensao_min)/2 as L2_forma_tensao, (L3_forma_tensao_max + L3_forma_tensao_min)/2 as L3_forma_tensao
                     FROM afundamento
                     WHERE id > $prevNum AND id < $a');

        $tabelas_intervalos_afunda1 = ($con->query("SELECT * FROM afunda_$a");

        while ($row = $tabelas_intervalos_afunda->fetch(PDO::FETCH_ASSOC)) {
            $array_forma_onda_fase1_afund[] = $row['L1_forma_tensao'];
            $array_forma_onda_fase2_afund[] = $row['L2_forma_tensao'];
            $array_forma_onda_fase3_afund[] = $row['L3_forma_tensao'];
        $prevNum = $a;

So as u can see, i have an if statement in a for loop, what i'm wishing to do is to create one set of:

    $array_forma_onda_fase1_afund[] = $row['L1_forma_tensao'];
    $array_forma_onda_fase2_afund[] = $row['L2_forma_tensao'];
    $array_forma_onda_fase3_afund[] = $row['L3_forma_tensao'];

every time the if statement is runned. I was trying replacing this in the original code:

    $array_forma_onda_fase1_afund_$a[] = $row['L1_forma_tensao'];
    $array_forma_onda_fase2_afund_$a[] = $row['L2_forma_tensao'];
    $array_forma_onda_fase3_afund_$a[] = $row['L3_forma_tensao'];

so as$a is changed everytime the if statement is accessed, i could have a different set of these arrays for everytime the if statement is accessed, but php doesn't accept this and i wouldn't have a very good result, though if i can reach it i would be pleased.

But my goal is to get:

        $array_forma_onda_fase1_afund_1[] = $row['L1_forma_tensao'];
        $array_forma_onda_fase2_afund_1[] = $row['L2_forma_tensao'];
        $array_forma_onda_fase3_afund_1[] = $row['L3_forma_tensao'];
        $array_forma_onda_fase1_afund_2[] = $row['L1_forma_tensao'];
        $array_forma_onda_fase2_afund_2[] = $row['L2_forma_tensao'];
        $array_forma_onda_fase3_afund_2[] = $row['L3_forma_tensao'];


where the last number represents the array retrieved for the n-th time the if statement runned. Does someone have a tip for it?

Thanks in advance! Would appreciate any help.


As asked, my real world terms is as follows:

I have a table from which i need to take all the data that is inside a given interval. BUT, there's a problem, my data is a sine function whose amplitude may change indefinite times (the data bank is entered by the user) and, when the amplitude goes inside that interval, i need to make some operations like getting the least value achieved while the data was inside that interval and some other parameters, for each interval separately, (That's why i created all those tables.) and count how many times it happpened. So, in order to make one of the operations, i need an array with the data for each time the databank entered by the user goes in that interval (given by the limits of the create query.).

If i were not clear, just tell me please!


Here's the image of part of the table i'm working with:

so, when the sine gets inside the interval i need, it can be seen by the L1_RMS column, who accuses it, so it's when i need to get the interval data until it gets outside the interval. But it may happens as many times as this table entered by the user brings it on and we need to bear in mind that i need all the intervals separately to deal with the data of each one.




Physics uh?

You can do what you wanted with the arrays, it's not pretty, but it's possible. You can dynamically name your arrays with the _$a in the end, Variables variables, such as:

${"array_forma_onda_fase3_afund_" . $a}[] = "fisica é medo";

People are also looking for solutions to the problem: MySQL date comparison from PHP


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.