我在 Java 中有一个数组,我不知道有多少个职位
ArrayList<String> postparameters2send = new ArrayList<String>();
for(int i=0; i<4; i++){
try{
for (int e=0;e<10;e++){
if(group[i][e]==true){
postparameters2send.add("1");
}else if(group[i][e]==false){
postparameters2send.add("0");
}
}
}catch(Exception e){
}
}
Post post = new Post();
JSONArray jdata=post.getServerData(postparameters2send, "http://www.xxxxx.xx/xxx.php");//"xxxx" isnt the real name
我尝试这样做,
$filtros[]= ($_POST);//here i send the array to that variable
print_r(json_encode($_POST))
foreach ($filtros as $valor) {
$envio= mysql_query("INSERT INTO user_trans(id_usuario,id_transporte)
VALUES('206', '".$valor."')");
}
print_r(json_encode($filtros));
11-13 19:41:06.151: E/log_tag(332): Cadena JSon [{"1":"1"}]//$_POST
11-13 19:41:06.191: E/log_tag(332): Cadena JSon [{"1":"1"}]//$filtros
好吧,问题是$filtros
只能获得数组的 1°位置
所以只插入一次,数组的第一个元素忽略其余部分
Cadena JSon {"1":"1","1":"0"} //i sent array[3]
Cadena JSon {"1":"1","1":"0","0":"1"} //i sent array[5]
答案 0 :(得分:0)
要在PHP中发布值数组,您需要使用方括号[]附加键(表单名称),否则将只使用一个值。
答案 1 :(得分:0)
你能试试吗,
echo "<pre>";
print_r($_POST);
echo "</pre>";
$filtros[]= $_POST;//here i send the array to that variable
//print_r(json_encode($_POST));
foreach ($_POST as $key=>$valor) {
$envio= mysql_query("INSERT INTO user_trans (id_usuario,id_transporte)
VALUES('206', '".$valor."')");
}
print_r(json_encode($filtros));
11-14 14:15:45.233: E/log_tag(814): Cadena JSon <pre>Array
11-14 14:15:45.233: E/log_tag(814): (
11-14 14:15:45.233: E/log_tag(814): [1] => 1
11-14 14:15:45.233: E/log_tag(814): [1] => 0
11-14 14:15:45.233: E/log_tag(814): [0] => 0
11-14 14:15:45.233: E/log_tag(814): )
11-14 14:15:45.233: E/log_tag(814): </pre>{"1":"1","1":"0","0":"0"}