2017-11-10 72 views
-1

我需要创建一个函数接收一个变量并将其作为json发送。我预计JSON输出是:我需要发送这个变量在PHP

{ "item": [ 
    { 
     "a": "string", 
     "b": "string", 
     "c": 0 
    }], 
    "country": 0 
} 

这是我在PHP函数:

$postData['item'] = 
array(
    'a' => "12345", 
    'b' => "98765", 
    'c' => 0000, 
); 
$postData['country']= 1; 

这产生:

<pre> 
array(2) { 
    ['item'] => array(1) { 
     ['sn'] => string(13) '9597/08639650' 
    } 
    ['country'] => int(717808) 
} 
</pre> 

我也越来越其中指出的错误: ["item is not of a type(s) array"]

我在这里做错了什么?

Regards

回答

0

如果没有更多信息,我无法复制您的问题。这里是你的代码working repl.it。它显示没有问题了预期的结果:

<pre>array(2) { 
    ["item"]=> 
     array(3) { 
      ["a"]=> 
      string(5) "12345" 
      ["b"]=> 
      string(5) "98765" 
      ["c"]=> 
      int(0) 
     } 
    ["country"]=> 
     int(1) 
} 
</pre> 

而且json_encode($postData)

string(52) "{"item":{"a":"12345","b":"98765","c":0},"country":1}" 

如果你不能得到这个工作,请发表关于什么的$postData的实例,当你之间发生的更多信息打印它。