提问人:Adam Makowski 提问时间:8/1/2018 最后编辑:mickmackusaAdam Makowski 更新时间:8/1/2018 访问量:60
如何使用逗号在单个字符串中循环 10 倍的值?
How to for loop 10 times values in single string using comma?
问:
如何使用循环PHP创建如下所示的相同结果?$d4_c_d_v
注意:末尾必须以逗号结尾,不带逗号。$d4_c_d_v
$d4_c_d_v =
$g_demographics_value_0['id'] .','.
$g_demographics_value_1['id'] .','.
$g_demographics_value_2['id'] .','.
$g_demographics_value_3['id'] .','.
$g_demographics_value_4['id'] .','.
$g_demographics_value_5['id'] .','.
$g_demographics_value_6['id'] .','.
$g_demographics_value_7['id'] .','.
$g_demographics_value_8['id'] .','.
$g_demographics_value_9['id'];
到目前为止,我已经尝试过:
for ($i = 0; $i <= 9; $i++) {
$d4_c_d_v .= ${'g_demographics_value_'.$i[$i]};
}
echo $d4_c_d_v;
答:
1赞
mickmackusa
8/1/2018
#1
我个人不会以这种方式设置我的数据存储,但变量可以解决问题。
条件是检查非零值。因此,除了第一次迭代之外,每次迭代都会添加逗号。$i
代码:(演示)
$g_demographics_value_0['id'] = 'a';
$g_demographics_value_1['id'] = 'b';
$g_demographics_value_2['id'] = 'c';
$g_demographics_value_3['id'] = 'd';
$g_demographics_value_4['id'] = 'e';
$g_demographics_value_5['id'] = 'f';
$g_demographics_value_6['id'] = 'g';
$g_demographics_value_7['id'] = 'h';
$g_demographics_value_8['id'] = 'i';
$g_demographics_value_9['id'] = 'j';
$d4_c_d_v = '';
for($i = 0; $i < 10; ++$i) {
if ($i) {
$d4_c_d_v .= ',';
}
$d4_c_d_v .= ${"g_demographics_value_$i"}['id'];
}
echo $d4_c_d_v;
输出:
a,b,c,d,e,f,g,h,i,j
评论