我发送php数组为json格式,但我无法解码值。 这里是我做的阵列json解码错误
$ads = $atts['ads'];
if (sizeof($ads) > 0) {
foreach($ads as $social_item) {
$sdbr = $social_item['sidebar'];
$pno = $social_item['no'];
$out[$sdbr] = $pno;
}
}
哪个输出
array (
'Full width ad 1' => 2,
'sidebar-1' => 3,
)
现在我有JSON编码它
$myJSON = json_encode($out);
的JSON格式值{"Full width ad 1":2,"sidebar-1":3}
然后我将其通过数据属性值
echo "<div data-ad = '$myJSON' class='ash_loadmore'><span>LOAD MORE</span>
</div>";
,我已经得到了
$ad = $_POST['ad'];
array (
'Full width ad 1' => '2',
'sidebar-1' => '3',
)
所以,现在时间解码输出
$out = json_decode($ad,TRUE);
var_dump($out); // Returns NULL although the array value is present
但如果我把JSON格式的数据,它工作正常
$out = json_decode('{"Full width ad 1":2,"sidebar-1":3}',TRUE);
var_dump($out);
我的了在json编码之前怀疑数组array(2) { ["Full width ad 1"]=> int(2) ["sidebar-1"]=> int(3) }
值是int
但我的价值为string
array(2) { ["Full width ad 1"]=> string(1) "2" ["sidebar-1"]=> string(1) "3" }
我在做什么错?
你说'$ ad = $ _POST ['ad'];'?你如何传递数据?通过ajax?因为'data-ad'永远不会通过正常的表单发布(刷新页面)。所以如果你使用ajax,那么你的html中也要添加这个代码 –
如果'$ ad'已经输出'array(...)',那不是JSON了。 – deceze
我使用ajax发送,它在wordpress环境中 –