php - How to create a dynamic array variable -
i have these arrays , want loop through every array creating dynamic variable put name in foreach loop automatically this
foreach ($activities.$i &$activity) { //$i = 1,2,3,4.. //code } //activities $activities1 = $_post["activities1"]; $activities2 = $_post["activities2"]; $activities3 = $_post["activities3"]; $activities4 = $_post["activities4"];
easier method use array naming hack:
<input name="activities[1]" ..> <input name="activities[2]" ..> <input name="activities[3]" ..>
which makes $_post['activities']
array:
foreach($_post['activities'] $i => $value) { // $i -> 1,2,3,4 }
but if insist on embedding index inside key's name, then:
foreach(range(1,4) $i) { foreach($_post["activities{$i}"] $value) { ... } }
Comments
Post a Comment