0

How to define array in php like array1[] array2[] using for loop like instead of a1 or a2 I can write ai and its in for loop is it possible?

Like

for($i=0;$i<10;$i++)
{
     $ai[$i]="hello'";
}

so I would like to say as like

a1[1]=Hello
Habib
  • 591
  • 8
  • 29
Vinit Mapari
  • 439
  • 3
  • 7
  • 15
  • 1
    For the sake of code sanity please refrain from doing this, ever. – MonkeyZeus Sep 05 '18 at 17:02
  • 2
    Don't do this! Just `$a[$i][]` or something if you must. – AbraCadaver Sep 05 '18 at 17:25
  • Welcome to Stack Overflow! Your question seems being unclear and leading already to confusing answers too. Perhaps try to write your question detailed in your own language and translate it with http://translate.google.com or https://www.bing.com/translator. Please edit the question then instead of adding the translation as comment. Thanks! – David Sep 05 '18 at 23:44

2 Answers2

0

You can access variables dynamically using this:

${"a$i"}[$i] = 'hello';
Phiter
  • 14,570
  • 14
  • 50
  • 84
0

You could use a dynamic var

 $ai.$i[$i] = 'Hello';

or

 $ai.$i[$your_index] = 'Hello';

but be careful when doing this .. in this case it is difficult to keep in control of the variables .. is easy to make a mistake

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107