PHP访问json对象中的数组



我用JQuery创建了一个JSON对象,看起来像这样:

var massUpdateDetails = {
checkone: $('#checkone').val(),
checktwo: $('#checktwo').val(),
details: [{
detailone: $('#detailone').val(),
detailtwo: $('#detailtwo').val()
}],
locs: [{
locone: $('#locone').val(),
loctwo: $('#loctwo').val()
}]
}

当我将massUpdateDetails对象发送到PHP时,我可以像这样打印出对象:

<?php
if(isset($_POST['massUpdateDetails'])){
$value = $_POST['massUpdateDetails'];
}
print_r($value);
?>

当使用print_r($value)时,我看到的是:

Array
(
[checkone] => checkoneInfo
[checktwo] => value1,value2
[details] => Array
(
[0] => Array
(
[detailone] => details of one
[detailtwo] => details of two
)
)
[locs] => Array
(
[0] => Array
(
[locone] => loc one
[loctwo] => loc two
)
)
)

我试图设置各种值PHP变量如下:

$checkone= isset($value['checkone']) ? $value['checkone'] : ''; // <-- no problem getting this set
$detailone = isset($value['details']['detailone']) ? $value['details']['detailone'] : ''; // <-- problem is here
echo $detailone;

正如您在上面看到的,我在为$detailone设置变量时遇到了问题。

当我回显$detailone时,我在控制台中没有得到错误。

我做错了什么,我怎么能解决它?

$value['details']是一个数组,所以你必须访问每个元素作为一个数组元素,例如$value['details'][0]['detailone']

最新更新