如何从php接收javascript中的此类数据



在json代码中,原始形式为

data: [
{ x: 62, y: -78 },
{ x: -0, y: 74 },
]

我想从php值中接收坐标值。所以我会制作类似的json代码

data: [fromphpvalue]

那么我如何从php中生成这种类型的数据呢?我试过

$fromphpvalue = json_encode(array('x' => 62, 'y' =>-78), array('x' => 0, 'y' =>74));
echo "<script>var fromphpvalue= '$fromphpvalue';</script>";

但它不起作用。

您需要将值传递给json_encode的第一个参数。

$fromphpvalue = json_encode([['x' => 62, 'y' => -78], ['x' => 0, 'y' => 74]]);
echo "<script>var fromphpvalue = $fromphpvalue;</script>";

最新更新