How to get the array in php
Passing form data through the array can save the business attribute relationship between the data , for example, there are many Students, and each Student has form information such as name, age, gender, hobbies, etc. After submitting the form, each student needs to be processed or saved. In this way, it is definitely necessary to establish an association relationship for these attribute forms of each student. One way is to add a special mark to the name of the attribute form to identify it. Using an array to pass the form can make the form data more structured.
(Recommended learning: PHP video tutorial)
Examples are as follows:
<input type="hidden" name="msginfo[name][]" value="张三"/> <input type="hidden" name="msginfo[phonenum][]" value="111111111"/> <input type="hidden" name="msginfo[name][]" value="李四"/> <input type="hidden" name="msginfo[phonenum][]" value="222222222"/>
php code:
<?php $msgInfos = $_POST['msginfo']; $phoneNums = $msgInfos['name']; // 为array(-=>张三,1=>李四) $phoneNums = $msgInfos['phonenum']; // 为array(0=>111111111,1=>222222222)
Example One
<?php if(isset($_POST['submit'])){ $users = $_POST['user']; foreach($users as $key=>$val){ echo 'user ',$key,' = ',$val,'<br />'; } } ?>
<form method="post"> zhangsan <input type="text" name="user[zhangsan]" value="0" /><br /> lisi <input type="text" name="user[lisi]" value="1" /><br /> wangwu <input type="text" name="user[wangwu]" value="2" /><br /> zhaoliu <input type="text" name="user[zhaoliu]" value="3" /><br /> <input type="submit" name="submit" value="提交" /> </form>
Example two
<form method="post"> <? for($i=0;$i<10;$i++){ ?> <input type="checkbox" name="interests[]" value="<?=$i?>">test<?=$i?><br> <? } ?> <input type="submit"> </form> <?php <code class="php keyword">if(isset($_POST)){ foreach($_POST as $key => $val){ if(is_array($val)){ foreach($val as $v2){ echo "$v2<br>"; } } } } ?> </code>
The above is the detailed content of How to get an array in php. For more information, please follow other related articles on the PHP Chinese website!