我有由动态表单输入生成的多维数组,用户可以添加这些数组来添加奖项、工作和教育等内容。
当我收集数组数据并将其推送到 .csv 时,它会打印数组分组,如下所示 FinanceWaterlooTheodore2015TreasurerTorontoGary2014
。
是否可以标记数组循环结束的点,以便它更清晰,并且更像这样 FinanceWaterlooTheodore2015//TreasurerTorontoGary2014?
$unitLevelInvolvement = $_POST["unitLevelInvolvement"];
$unitInvolvementValue = "";
$i;
foreach($unitLevelInvolvement as $involvement)
{
$i++;
$unitInvolvementValue .= $involvement;
}
echo $unitInvolvementValue;
<div name="unitLevelInvolvement" id="unitLevelInvolvement">
<input type="text" class="two-lines-textbox" name="unitLevelInvolvement[]" placeholder="Position/Committee" onBlur="this.placeholder='Position/Committee'" onFocus="this.placeholder=''" onKeyUp="checkPage3()" />
<input type="text" class="two-lines-textbox" name="unitLevelInvolvement[]" id="oectaUnit_1" placeholder="Unit" onBlur="this.placeholder='Unit'" onFocus="this.placeholder=''" onKeyUp="checkPage3()" />
<div class="clearFix"></div>
<input type="text" class="two-lines-textbox" name="unitLevelInvolvement[]" id="unitPresident_1" placeholder="Unit President" onBlur="this.placeholder='Unit President'" onFocus="this.placeholder=''" onKeyUp="checkPage3()" />
<input type="date" class="two-lines-textbox" name="unitLevelInvolvement[]" id="unitYear_1" onKeyUp="checkPage3()" />
<input type="button" value="+" onClick="addUnitInvolvement()" />
</div>
</div><!-- end of unit-level-involvement div-->
<input type="submit" value="submit" />
</form><!--endForm-->
最佳答案
我知道您尝试每 4 次迭代放置一次分隔符(因为您有 4 个输入)。
请注意,只有当您恰好有 4 个输入时,此答案才有效。
你可以尝试这样的事情:
$unitLevelInvolvement = $_POST["unitLevelInvolvement"];
$unitInvolvementValue = "";
$i = 0;
foreach($unitLevelInvolvement as $involvement)
{
$unitInvolvementValue .= $involvement;
$i++;
if($i % 4 == 0){
$unitInvolvementValue .= "//";
}
}
echo $unitInvolvementValue;
关于javascript - PHP - 间隔数组迭代,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40179455/