解码 JSON 和无法访问的变量


Decoding JSON and inaccessible variables

在这个问题中,我在简化和形成一个有效的复杂JSON POST请求方面得到了帮助。 但是,我现在在服务器端遇到了一些奇怪的行为。

function postTour(){
  $post = json_decode($_POST['json'];
  $success = false;
  for ($i=0; $i<count($post); $i++){
    $filename = $post[i]['location']['filename'];
  }
}

在这里,$filename永远不会初始化,也永远不会在调试器中显示为变量。 $post返回格式的多级数组

$post[3]
   [0] =>
       location = [ 5 key/value pairs ]
       links = one to n arrays
   [1] =>
       location = [ 5 key/value pairs],
       links = one to n arrays

在调试器中,每个最外层的数组和位置数组都具有 stdClass 类型,而 links 数组的类型为 array[n] 。 但是,我无法访问$post中的任何信息。 这是为什么呢?

尝试将true作为第二个参数传递给json_decode(),以便将其转换为实际数组。

$post = json_decode($_POST['json'], true);