Home  >  Article  >  Backend Development  >  How to get an array in php

How to get an array in php

angryTom
angryTomOriginal
2019-10-26 13:36:283067browse

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"/>

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)

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;;
}
}
?>
<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!

Statement:
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