基于逗号分隔值的计数动态创建文本区域


create textarea dynamically based on count of comma separated values

数据库返回了这个数组

Array ( [0] => Array ( [id] => 3 [test] => alphy,poxy,jade,auma ) ) 

然后我用爆炸来分离的值

$options = explode(",", $test1['0']['test']);

结果是

 array(4) { [0]=> string(5) "alphy" [1]=> string(4) "poxy" [2]=> string(4) "jade" [3]=> string(4) "auma" }

然后我计算的数值

$count= substr_count($test1['0']['test'], ",")+1;

然后我现在尝试根据计数和值动态创建文本区域,例如文本区域1-alphy、文本区域2-poxy。。。

  for($i=0; $i<=$count;$i++){?>
            <textarea ><?php echo $options[$i]['test']?> </textarea>
    <?php }?>

以上并没有做到,相反,每个文本区域只有第一个字母,如a、p、j、a,而不是alphy、poxy、juma、auma。

我错过了什么?

只需迭代$options数组即可打印出文本区域,无需获取计数。

$test1 = array(array('id' => 3, 'test' => 'alphy,poxy,jade,auma')); 
$options = explode(",", $test1['0']['test']);
foreach ($options as $i => $option) {
    echo '<textarea name="textarea_' . $i . '">' . $option . '</textarea>';
}

当然,如果你真的想使用计数,你可以这样做:

$test1 = array(array('id' => 3, 'test' => 'alphy,poxy,jade,auma')); 
$options = explode(",", $test1['0']['test']);
$count = count($options);
for ($i = 0; $i < $count; $i++) {
    echo '<textarea name="textarea_' . $i . '">' . $options[$i] . '</textarea>';
}

编辑:考虑到对您问题的编辑,您似乎正在尝试访问每个选项的索引("测试")。但是,一旦它们被拆分成一个数组,它们就会变成简单的字符串,所以不需要像数组一样尝试访问它们。

获得第一个字母的原因是$x = 'alpha'; $x['test']的计算结果为$x[0],它访问字符串中的第一个字符,即a