Generate a new array dynamically with php

Asked

Viewed 52 times

0

How could I generate a new array in a loop? for example make the machine generate array1[], array2[]

I can create a variable like this with the following code:

${'var'.$i}
// E isso virar $var1, $var2

But what about an array like I would do? Thank you all and a strong hug!

1 answer

0

$array = [];
for ($i=0; $i <= 20 ; $i++) { 
    array_push($array,"var" . $i);
}

Just change the value 20 to the var number you need.

  • Opa valeu partner! Actually here he is creating new arrays? Or in case 20 variables within a single array? Because what I needed was actually the loop for or while, create for me a certain number of arrays and not variables within arrays, I do not know if I could explain straight hehe anyway thank you hugs!

  • A multidimensional array of help ?

  • Poxa to be quite honest I’m kind of new in programming mainly in php hehe, MAYBE it helps, I don’t know, but I’ve never used... Do you think it would be the only way? Alias what you say is Matrix right?

  • $var = array(); for ($i=0; $i <= 20 ; $i++) { array_push($var,array(["var" . $i]); } #If you need it, go to array 3 and get the first result. echo $var[3][0];

  • Man mto thanks for the return! This for me was very new hehe... It took me a while to thank you here because I wanted to test some things with the code you made, but I’m still a little lost... For example... Inside this is, how do I select an array that was generated inside, but not a specific one but a $i even number... and add some value to it? I do not know if it is already and ask too much if it is Sorry hehe anyway I am very grateful for the help!!! I’m trying from here hugs! o/

  • For example... add some content to it like: array_push(array(["var".$i]), "watermelon", "potato");... And access it maybe to printar or pebble print_r(array(["var".$i));... But apparently this method does not work hehe

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.