如何在PHP循环中动态创建变量名? [英] How do I dynamically create the variable name in a PHP loop?
问题描述
好,所以我有这个php foreach循环
Ok so i have this php foreach loop
<?php foreach ($step_count->rows as $step) { ?>
和$ step将是步骤1、2、3、4、5,直至总步骤
and $step will be the step numbers 1, 2, 3, 4, 5 up to the total steps
在循环中,我需要将循环中的图像的值设置为standard_image_1或任何存在的步骤...因此例如
within the loop i a need to set the value of the images within the loop to standard_image_1 or whatever step there is...so for example
<input value="<?php echo {$standard_image_"$step['number']"}; ?>" />
所以基本上我需要变量$ standard_image_1等依步骤而定,但我不知道执行此操作的正确语法
so basically i need the variable $standard_image_1 and so on depending on the steps but i dont know the correct syntax to do this
推荐答案
Look at the docs for "variable variables" - http://php.net/manual/en/language.variables.variable.php
<?php echo ${'standard_image_'.$step['number']}; ?>
这里是一个模型,使用您提供的详细信息: http://codepad.org/hQe56tEU
Here's a mock-up, using the details you've given: http://codepad.org/hQe56tEU
这篇关于如何在PHP循环中动态创建变量名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!