将Javascript数组保存到PHP

将Javascript数组保存到PHP,php,javascript,Php,Javascript,你好,我对javascipt很陌生,所以请解释清楚。我目前正在运行一个php页面,其中包括: upp.php 页面结果如下所示: function save(){ var oldItems = JSON.parse(localStorage.getItem('itemsArray')) || []; var newItem = {}; var num = document.getElementById("num").value; newItem[num] = { "methv":

你好,我对javascipt很陌生,所以请解释清楚。我目前正在运行一个php页面,其中包括:

upp.php

页面结果如下所示:

function save(){

var oldItems = JSON.parse(localStorage.getItem('itemsArray')) || [];

var newItem = {};
var num = document.getElementById("num").value;

newItem[num] = {
    "methv": document.getElementById("methv").value
    ,'q1': document.getElementById("q1").value,
    'q2':document.getElementById("q2").value,
    'q3':document.getElementById("q3").value,
    'q4':document.getElementById("q4").value,
    'comm':document.getElementById("comm").value
};
oldItems.push(newItem);
localStorage.setItem('itemsArray', JSON.stringify(oldItems));}

$.post('upp.php',{ items: JSON.stringify(oldItems) }, function(response) {
    window.location.href = "upp.php";
,,,,,,,,,,,,,,,,,,,,,,,,,,,“q3“,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,“U”、“q4”:“-”、“comm”:“}”{“1173627548:{methv:“不知道”,“q1:”,“q2:”,“q3:“U”,“q4:”,“comm:”}},{1173627548:{“methv:“不知道”,“q1:”,“q2:”,“q3:“U”,“q4:”,“comm:”}}},

我是否可以将这些信息保存到PHP中,并拆分数据,以便一次一个地对其进行操作,如循环或其他操作。例如:

第一次:

{“1173627548”:{“methv”:“不知道”、“q1”:“-”、“q2”:“-”、“q3”:“U”、“q4”:“-”、“comm”:“}

下一步:

{“1173627548”:{“methv”:“不知道”、“q1”:“-”、“q2”:“-”、“q3”:“U”、“q4”:“-”、“comm”:“}

等等


谢谢。

upp.php:

<?php 
$array = json_decode($_POST['items'], True);
foreach ($array as $key=>$line) {
    # $key is a number like 1173627548
    # and $Line is an array with methv, q1, q2, q3, q4, and comm
}
?>


这将向您显示它从JSON获得的数组。现在您可以处理数据了。

在upp.php中,您可以使用
JSON\u decode
函数将其转换回objectsHey David我在php页面中为这些项获得了一个未定义的索引。有什么想法吗?或者我必须导入类似jQuery的内容吗?测试您的jQuery AJAX调用。
<?php 
$array = json_decode($_POST['items'], True);
foreach ($array as $key=>$line) {
    # $key is a number like 1173627548
    # and $Line is an array with methv, q1, q2, q3, q4, and comm
}
?>