There is such a code (see below). You need to repeat it 5 times. The value of $ i must be reached in the variable $ urltitle2, that is, the first time is 1. With this, everything is clear <?php echo $urltitle2.$i; ?> <?php echo $urltitle2.$i; ?> How to do the same with values

 $control_menu_urltitle2[$language['language_id']]) $control_menu_url2[$language['language_id']] 

it is necessary that it was not 2 and 21, that is 2. $ i, should come

 $control_menu_urltitle21[$language['language_id']]) $control_menu_url21[$language['language_id']] 

This construction is wrong

 $control_menu_url2.$i.[$language['language_id']] 

Here is the code itself:

 <!-- language: lang-php --> <?php $i = 1; $k = 5; for ($i;$i>k;$i++) { ?> <div class="form-group sub-link"> <label class="col-sm-1 control-label" ><?php echo $urltitle2.$i; ?></label> <div class="col-sm-4"> <?php foreach ($languages as $language) { ?> <div class="input-group"><span class="input-group-addon"> <img src="view/image/flags/<?php echo $language['image']; ?>" title="<?php echo $language['name']; ?>" /></span> <input class="form-control" type="text" name="control_menu_urltitle2<?php echo $i;?>[<?php echo $language['language_id']; ?>]" value="<?php echo isset($control_menu_urltitle2[$language['language_id']]) ? $control_menu_urltitle2[$language['language_id']] : ''; ?>" /> </div> <?php } ?> </div> <label class="col-sm-1 control-label" ><?php echo $url; ?></label> <?php foreach ($languages as $language) { ?> <div class="col-sm-1"></div> <div class="col-sm-6"> <div class="input-group"><span class="input-group-addon"> <img src="view/image/flags/<?php echo $language['image']; ?>" title="<?php echo $language['name']; ?>" /></span> <input class="form-control" type="text" name="control_menu_url2<?php echo $i;?>[<?php echo $language['language_id']; ?>]" value="<?php echo isset($control_menu_url2[$language['language_id']]) ? $control_menu_url2[$language['language_id']] : ''; ?>" /> </div> </div> <?php } ?> </div> <?php } ?> 
  • use the array $array[2][$i] or just $array[$i] - Naumov

1 answer 1

For the variable variable name, use curly brackets:

 $control_menu_url21 = ['test' => 21]; $control_menu_url22 = ['test' => 22]; for ($i = 1; $i <= 3; ++$i) { echo ${'control_menu_url2'.$i}['test']; // выведет 21 22 } 

https://ideone.com/fYGzkd

In general, it's probably better to use an array for such purposes.