How to dynamically fill in php fixes?

2

I want to ask for your kind help since I am a newbie in php.

I have the following amounts:

$xcantidad1 = 1 ; $xcantidad2 = 2 ; $xcantidad3 = 2 ; $xcantidad4 = 3 ; 

I have a top of 10 lines.

How can I fill in the 10 rows with the amounts mentioned above and with a total of 4 elements (columns).

Like this: renglon1 = [1,2,2,3]

Sorry to ask something maybe silly, that would be dynamic formation of an arrangement?

Thank you.

    
asked by Semtia - JorgeLira 06.06.2018 в 06:04
source

1 answer

0
  

[...] That is, dynamically create this: xrenglon1 and inside have 1, 2, 2, 3, then line 2 so up to n lines.

A simple way is to use for loops where you can limit both the number of rows and the number of elements. Then assign the names of the variables at the time of adding the values of the array .

It would stay like this:

$xcantidad1 = 1;
$xcantidad2 = 2;
$xcantidad3 = 2; 
$xcantidad4 = 3;

$renglones = 10;
$elementos = 4;

for($i=1; $i <= $renglones; $i++){
    for($n=1; $n <= $elementos; $n++){
        ${"xrenglon$i"}[] = ${"xcantidad$n"};
    }
}

// para comprobar que la salida es correcta
print_r($xrenglon1);
print_r($xrenglon2);
print_r($xrenglon3);
print_r($xrenglon4);
//...
print_r($xrenglon10);

You can see it working in this example ( external link ).

    
answered by 06.06.2018 в 09:21