how can I create variables from a for php loop

0

I have 3 data cod-es, cod-a and counter What I want is that according to the meter n variables are generated Example

$campo1 = $_POST['dato'];
$campo2 = $_POST['dato2'];
    
asked by Rodrigo Frías Soto 22.06.2018 в 01:16
source

3 answers

1

In PHP you can name your variables dynamically:

$nombre = 'campo1';
$$nombre = 'valor1';
echo $campo1;

will print 'value1'

What you just did when you declare $$nombre is declare $campo1 .

If you have a counter that tells you how many fields you will receive for $ _POST, you can iterate over them. Let's say $ _POST is an array that contains

[
 'contador'=>4,
 'dato1'=>'pedro',
 'dato2'=>'juan',
 'dato3'=>'diego',
 'dato4'=>'alejandro'
]

You can do:

<?php
$SEUDO_POST=    [
     'contador'=>4,
     'dato1'=>'pedro',
     'dato2'=>'juan',
     'dato3'=>'diego',
     'dato4'=>'alejandro'
    ];

for($i=1;$i<=$SEUDO_POST['contador'];$i++) {
    $nombre ='dato'.$i;

    $$nombre = $SEUDO_POST[$nombre];
}

print_r([$dato1,$dato2,$dato3,$dato4]);

You can see it working in a fiddle .

Now ... this is not the only way to do it. You could also use extract $ SEUDO_POST

But it's a bad idea to use extract. Tomorrow comes someone to keep your code and without having an idea what contains $_POST can be found with:

extract($_POST);
echo $dato1;

And you will not have the slightest idea where $dato1 came from. Worse still, someone can send a variable by POST manipulating your form, and overwrite a variable that you already have declared, managing your backend indirectly.

In any case, whatever you need to do with $ field1 and $ field2, you can do it with a simple associative array , using $miarray['campo1'] instead of $campo1 .

    
answered by 22.06.2018 в 02:40
0

Since you gave the example of a $_POST , I'll give you an example of what you want to do using extract . The Manual warns not to use this function with values that are not trusted, unless you put the flag EXTR_SKIP , this is to prevent the risk of code manipulation. In that sense, the use of eval is much more dangerous.

What extract does is:

  

Import variables to the current symbol table from an array

     

extract in the PHP Manual

Since $_POST is an array, you can do it perfectly with it, as well as with any other array.

Let's see:

$count=3;
for ($i = 1; $i <= $count; $i++) {
    $k="llave$i";
    $v="valor$i";
    $_POST[$k]=$v;
}
extract($_POST, EXTR_SKIP);

echo $llave1.PHP_EOL;
echo $llave2.PHP_EOL;
echo $llave3.PHP_EOL;

Exit:

valor1
valor2
valor3

If you print print_r($_POST) you will see this:

Array
(
    [llave1] => valor1
    [llave2] => valor2
    [llave3] => valor3
)

And if you go through it in a loop:

foreach ($_POST as $llave){
    echo $llave.PHP_EOL;
}

Exit:

valor1
valor2
valor3

Demonstrations of the code

You can see a CODE DEMO used in the response, see a test using a normal array and do other tests.

I hope it's useful.

    
answered by 22.06.2018 в 05:20
0

Inside your loop with the example you place. You should have a "cap." You can use them within the loop or once the iteration has been done

$TOPE = 5; // Por ejemplo
for ($i = 0; $i <= $TOPE; $i++) {
   // Cuando sea 0, buscará dato, caso contrario seria dato1, dato2... hasta el tope
   $search = "dato" . ($i == 0 ? '' : $i);
   if (!isset($_POST[$search])){
      break; // En caso de que no exista finalice el bucle
      // O puedes continuar si hay un salto (depende de tu logica)
      // continue;
   }
   $$search = $_POST[$search]; //Acá se crean las variables.
   print_r ($$search);  // print_r de las variables dentro del bucle
}
// print_r de las variables fuera del bucle
print_r ($dato);
print_r ($dato1);
print_r ($dato2);
print_r ($dato3);
print_r ($dato4);
print_r ($dato5);
    
answered by 22.06.2018 в 13:49