我有多维数组,这些数组是由动态表单输入生成的,用户可以添加这些数组来添加奖励,工作和教育等内容。
当我收集数组数据以将其推送到.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-->
答案 0 :(得分:1)
我知道你试图每4次迭代放一个分隔符(因为你有4个输入)。
注意,只有恰好有4个输入时,此答案才有效。
您可以尝试这样的事情:
$unitLevelInvolvement = $_POST["unitLevelInvolvement"];
$unitInvolvementValue = "";
$i = 0;
foreach($unitLevelInvolvement as $involvement)
{
$unitInvolvementValue .= $involvement;
$i++;
if($i % 4 == 0){
$unitInvolvementValue .= "//";
}
}
echo $unitInvolvementValue;
答案 1 :(得分:0)
首先,您需要创建&#34;群组&#34;因为你还没有。您可以通过创建具有name
属性的多维数组来完成此操作。你的HTML看起来像这样:
<div name="unitLevelInvolvement" id="unitLevelInvolvement">
<input type="text" class="two-lines-textbox" name="unitLevelInvolvement['group1'][]" placeholder="Position/Committee" onBlur="this.placeholder='Position/Committee'" onFocus="this.placeholder=''" onKeyUp="checkPage3()" />
<input type="text" class="two-lines-textbox" name="unitLevelInvolvement['group1'][]" 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['group2'][]" 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['group2'][]" id="unitYear_1" onKeyUp="checkPage3()" />
<input type="button" value="+" onClick="addUnitInvolvement()" />
</div>
</div><!-- end of unit-level-involvement div-->
然后你可以像这样迭代数组:
$unitInvolvementValue = array();
foreach($unitLevelInvolvement as $group)
{
$groupInvolvement = "";
foreach($group as $involvement)
{
$groupInvolvement .= $involvement;
}
$unitInvolvementValue[] = $groupInvolvement;
}
echo implode("//", $unitInvolvementValue );
我使用implode()
方法,因为它只在值之间添加了分隔符,而不是在字符串的开头或结尾。