How to traverse a variable with an internal loop without the external one affecting it in PHP?

0

I have a form that traverses values of five machines (machines) through a foreach that takes all the values that are sent, followed by checking which termination has the name of the text field and assigning it to the corresponding variable according to a comparison of patterns:

To correctly save the value and not have something like name_1, color_1, etc., before filling, I delete what is from the bottom script to the end.

This is the function:

function registerMachines(){
    foreach($_POST as $item => $value){
        if(preg_match('/_1$/', $item)){
            // Le saco los guiones 
            $item = explode("_", $item);
            // Uso solo la parte que tenga el item
            $item = $item[0];
            $data_1[$item] = $value;
        } elseif(preg_match('/_2$/', $item)){
            $item = explode("_", $item);
            $item = $item[0];
            $data_2[$item] = $value;
        } elseif(preg_match('/_3$/', $item)){
            $item = explode("_", $item);
            $item = $item[0];
            $data_3[$item] = $value;
        } elseif(preg_match('/_4$/', $item)){
            $item = explode("_", $item);
            $item = $item[0];
            $data_4[$item] = $value;
        } elseif(preg_match('/_5$/', $item)){
            $item = explode("_", $item);
            $item = $item[0];
            $data_5[$item] = $value;
        } else {
            $configData[$item] = $value;
        }
    }
}

What did you want?

To be able to optimize the filling of the arrays so that variables are created by the machine I have, thus avoiding creating each array (data_1, data2, etc). Already make the code here your explanation.

$machines = [1, 2, 3, 4, 5];
    foreach($_POST as $item => $value){
        echo "+++".$item."<br>";
        foreach($machines as $machine) {
            $match = '/_'.$machine.'$/';
            if(preg_match($match, $item)){
                $item = explode("_", $item);
                $item = $item[0];
                ${"machineData_".$machine}[$item] = $value;
            } else {
                echo $item."<br>";
                $configData[$item] = $value;
            }
        }
    }

How it works?

1 --- I go through all the values of the $ _POST.

2 --- I go through each machine internally ($ machine).

3 --- I create the pattern to check based on the value of the loop.

4 --- If it matches I eliminate the end of the $ item (_1, _2, _3, etc).

5 --- I create the variable (array) with the syntax $ {var. $ machine} and assign it the value.

What is the problem ?:

When I browse the POST array in the ELSE condition, all the variables are saved repeatedly, that is, they are saved with the _ already removed by the explode, which causes the configdata array to be filled with the data obtained from the machines.

What do I want?

I would like to be able to go through each POST value and only the values that have no underscore, which are only 4, will be saved in the config data.

Try placing them in an array =

$data = ["name", "space", "weight", "lift"];

Test data (obtained from POST)

["name"]=> string(5) "WWWWW"  ["space"]=> string(4)  ["weight"]=> int(2) 66  ["lift"]=> string(5) "WQWWW" ["RH1_1"]=> string(1) "3" ["RH2_1"]=> string(1) "3" ["RV1_1"]=> string(1) "3" ["RFO_1"]=> string(1) "3" ["CBO_1"]=> string(1) "3" ["OVA_1"]=> string(1) "3" ["INC_1"]=> string(1) "3" ["LBT_1"]=> string(1) "3" ["BNC_1"]=> string(1) "3" ["ACAB_1"]=> string(1) "3" ["AST_1"]=> string(1) "3" ["BSA_1"]=> string(1) "3" ["MRN_1"]=> string(1) "3"
    
asked by Victor Alvarado 16.02.2018 в 19:40
source

2 answers

1

You can try the following:

<?php
class OptimizeMachines {

    private static $counter = 1;

    public static function register($number_of_machines=5) {
        //self::$n_machines = $number_of_machines;

        foreach($_POST as $item => $value) {

            // Le saco los guiones
            $item = explode("_", $item);

            // Uso solo la parte que tenga el item
            $item = $item[0];
            ${"data_".self::$counter}[$item] = $value;

            self::$counter++;

            //reinicializa contador
            if (self::$counter > $number_of_machines) {
                self::$counter = 1;
                break;
            }
        }
    }

} //class

It's a first idea, it allows you to vary the number of machines. If you add a set of data for tests, I'd be more sure.

    
answered by 16.02.2018 в 20:13
0

The final code was this:

        $ConfigData = array();
        $machines=[1, 2, 3, 4, 5];
        $mainData = ["name", "space", "weight", "lift"];
            foreach($_POST as $item => $value){
                if (in_array($item, $mainData)) {
                    $ConfigData[$item] = $value;
                } else {
                    foreach($machines as $machine) {
                        $match = '/_'.$machine.'$/';
                        if(preg_match($match, $item)){
                            $item = explode("_", $item);
                            $item = $item[0];
                            ${"MachineData_".$machine}[$item] = $value;
                        } 
                    }
                }
            }
    
answered by 17.02.2018 в 01:29