如何在PHP中迭代由jquery(ajax)发布的数组?

时间:2015-11-12 20:17:57

标签: php jquery

这是我通过AJAX发送给PHP的帖子的var_dump

array (size=1)
  'myArray' => 
    array (size=3)
      0 => string 'event=100%20Yard%20Back%20Stroke&swimmername=john doe&lane=1&time=00:00:00&place=0&scoreA=0&scoreB=0&place2=0&time2=00:00:00&lane2=2&swimmername2=john doe' (length=164)
      1 => string 'event=100%20Yard%20Back%20Stroke&swimmername=john doe&lane=3&time=00:00:00&place=0&scoreA=0&scoreB=0&place2=0&time2=00:00:00&lane2=4&swimmername2=john doe' (length=164)
      2 => string 'event=100%20Yard%20Back%20Stroke&swimmername=john doe&lane=5&time=00:00:00&place=0&scoreA=0&scoreB=0&place2=0&time2=00:00:00&lane2=6&swimmername2=john doe' (length=164)

如何迭代每个元素并为11个元素的每一行分配3个值

$event =  event=100 Yard Back Stroke 
$swimmername = john doe
$lane = 5
.. etc

所以我可以将每一行(本例中为3)插入我的数据库

PHP代码

var_dump($_POST);

foreach($_POST as $data)
{


    if(isset($data['event'])){$event = $data['event'];}
    if(isset($data['swimmername'])){$name = $data['swimmername'];}
    if(isset($data['lane'])){$lane =  $data['lane'];}
    if(isset($data['place'])){$place = $data['place'];}
    if(isset($data['time'])){$time =  $data['time'];}
    if(isset($data['scoreA'])){$score = $data['scoreA'];}
    if(isset($data['scoreB'])){$score2 =  $data['scoreB'];}
    if(isset($data['time2'])){$time2 = $data['time2'];}
    if(isset($data['lane2'])){$lane2 =  $data['lane2'];}
    if(isset($data['place2'])){$place2 =  $data['place2'];}
    if(isset($data['swimmername2'])){$name2 = $data['swimmername2'];}


}

1 个答案:

答案 0 :(得分:0)

数组的每个元素都是一个字符串。 将爆炸()与'&'一起使用作为将每个字符串放入各个元素的数组中的分隔符。然后,将新数组的元素分配给程序其余部分所需的变量。