Javascript 从JS到PHP的对象数组

Javascript 从JS到PHP的对象数组,javascript,php,arrays,json,ajax,Javascript,Php,Arrays,Json,Ajax,我正面临一个问题,我已经试着解决了几个小时了,我希望你能帮我解决。所以从一开始,我就有一个JavaScript算法来生成变量,我想保存到数据库并在网站上打印给用户。为此,我认为我将生成一个对象数组,一次处理所有变量(它生成用户想要的任意多的变量,所以有时它可以是很少的,有时甚至是更多的)。我认为用json发送和处理会很容易,然后在PHP中可以作为普通变量访问。这是我的测试数据: [[{"id_day":1},{"exercise":"some test exercise1"},{"repeats

我正面临一个问题,我已经试着解决了几个小时了,我希望你能帮我解决。所以从一开始,我就有一个JavaScript算法来生成变量,我想保存到数据库并在网站上打印给用户。为此,我认为我将生成一个对象数组,一次处理所有变量(它生成用户想要的任意多的变量,所以有时它可以是很少的,有时甚至是更多的)。我认为用json发送和处理会很容易,然后在PHP中可以作为普通变量访问。这是我的测试数据:

[[{"id_day":1},{"exercise":"some test exercise1"},{"repeats":2},{"energyWorth":300}],
[{"id_day":1},{"exercise":"some test exercise2"},{"repeats":2},{"energyWorth":300}],
[{"id_day":2},{"exercise":"some test exercise3"},{"repeats":2},{"energyWorth":300}],
[{"id_day":2},{"exercise":"some test exercise4"},{"repeats":2},{"energyWorth":300}],
[{"id_day":3},{"exercise":"some test exercise5"},{"repeats":2},{"energyWorth":300}],
[{"id_day":3},{"exercise":"some test exercise6"},{"repeats":2},{"energyWorth":300}],
[{"id_day":4},{"exercise":"some test exercise7"},{"repeats":2},{"energyWorth":300}],
[{"id_day":4},{"exercise":"some test exercise8"},{"repeats":2},{"energyWorth":300}],
[{"id_day":5},{"exercise":"some test exercise9"},{"repeats":2},{"energyWorth":300}],
[{"id_day":5},{"exercise":"some test exercise10"},{"repeats":2},{"energyWorth":300}],
[{"id_day":6},{"exercise":"some test exercise11"},{"repeats":2},{"energyWorth":300}],
[{"id_day":6},{"exercise":"some test exercise12"},{"repeats":2},{"energyWorth":300}]]
我在脚本中使用ajax通过以下代码发送此对象数组:

$.ajax({
    url: 'test2.php',
    type: 'post',
    data: {"data_js" : JSON.stringify(plan)},
    success: function(data) {

    }
});
希望通过运行以下代码在名为test2.PHP的PHP文件中使用em:

if (isset($_POST["data_js"])) {
$data_js = $_POST["data_js"];
$data_js = json_decode($data_js);

var_dump($data_js);
var_dump($data_js[0][1]);
}
因此,我成功地将我的对象数组发送到名为$data_js的变量,并用json解码em,以便在PHP中使用em。问题是我的阵列现在看起来像这样:

array(20) {
  [0]=>
  array(4) {
    [0]=>
    object(stdClass)#1 (1) {
      ["id_day"]=>
      int(1)
    }
    [1]=>
    object(stdClass)#2 (1) {
      ["exercise"]=>
      string(37) "some test exercise1"
    }
    [2]=>
    object(stdClass)#3 (1) {
      ["repeats"]=>
      int(2)
    }
    [3]=>
    object(stdClass)#4 (1) {
      ["energyWorth"]=>
      int(300)
    }
  }
  [1]=>
  array(4) {
    [0]=>
    object(stdClass)#5 (1) {
      ["id_day"]=>
      int(1)
    }
    [1]=>
    object(stdClass)#6 (1) {
      ["exercise"]=>
      string(38) "some test exercise2"
    }
    [2]=>
    object(stdClass)#7 (1) {
      ["repeats"]=>
      int(2)
    }
    [3]=>
    object(stdClass)#8 (1) {
      ["energyWorth"]=>
      int(300)
    }
  }
等等。问题是我必须访问每个变量,就像在JS中一样,我可以运行数据[0]。我试图使用var_dump($data_js[0][1])作为例子;这给了我想要得到的变量,但格式如下:

object(stdClass)#2 (1) {
  ["exercise"]=>
  string(37) "some test exercise1"
}

这与我想要的相去甚远。我可能使用了错误的方法,第一次这么做,所以如果你们有想法或者告诉我我做错了,我会非常高兴。似乎我在这整个JSON-JSON-PHP事件中迷失了方向。提前谢谢你们

您应该使用
json\u decode($data,true)


第二个参数确保数据将以数组格式解码。

json\u decode($data\u js,true)
var\u dump($data\u js[0][1]>练习)?为什么不生成一个更合适的数组作为
[{“id_day”:1,“exercise”:“一些测试exercise1”,“repeats”:2,“energyWorth”:300},
在这种情况下,它将是
$data\u js[$i]->exercise
…Matteo似乎我可以通过它做到这一点,然后:$var1=$data[0][1];echo内爆(“,$var1);,但不确定这是否是一个好的练习@splash58很遗憾,不起作用,但正如你所说,Idk为什么我没有像你说的那样生成数组(我的错误,将修复它并尝试你的方法)@Sinner“不起作用”是什么意思-