How to get an array in php

angryTom
Release: 2023-02-27 19:04:02
Original
2976 people have browsed it

How to get an array in php

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"/>
Copy after login

php code:

<?php
 $msgInfos = $_POST[&#39;msginfo&#39;];
 $phoneNums = $msgInfos[&#39;name&#39;]; // 为array(-=>张三,1=>李四)
 $phoneNums = $msgInfos[&#39;phonenum&#39;]; // 为array(0=>111111111,1=>222222222)
Copy after login

Example One

<?php
if(isset($_POST[&#39;submit&#39;])){
$users = $_POST[&#39;user&#39;];
foreach($users as $key=>$val){
  echo &#39;user &#39;,$key,&#39; = &#39;,$val,&#39;<br />&#39;;
}
}
?>
Copy after login
<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>
Copy after login

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>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!