解码JSON和无法访问的变量

时间:2012-06-20 19:55:12

标签: php json

this问题中,我帮助简化并形成了有效的复杂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内的任何信息。这是为什么?

1 个答案:

答案 0 :(得分:4)

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

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