我正在使用以下代码来解码json数组
$json_contacts ='[{"addr_name":"1","addr_phone":"010-1111-1111"},{"addr_name":"2","addr_phone":"010-1111-1112"},{"addr_name":"3","addr_phone":"010-1111-1113"},{"addr_name":"4","addr_phone":"010-1111-1114"}]';
$contact = json_decode($json_contacts, true);
echo $json_contacts;
echo "\n";
echo $contact;
echo "\nTHE END";
我得到以下结果
[{"addr_name":"1","addr_phone":"010-1111-1111"},{"addr_name":"2","addr_phone":"010-1111-1112"},{"addr_name":"3","addr_phone":"010-1111-1113"},{"addr_name":"4","addr_phone":"010-1111-1114"}]
THE END
为什么我的JSON_DECODE没有正确解码数组?
答案 0 :(得分:3)
试试这个:
<?php
$json_contacts ='[{"addr_name":"1","addr_phone":"010-1111-1111"},{"addr_name":"2","addr_phone":"010-1111-1112"},{"addr_name":"3","addr_phone":"010-1111-1113"},{"addr_name":"4","addr_phone":"010-1111-1114"}]';
$contact = json_decode($json_contacts, true);
echo $json_contacts;
echo "\n";
echo '<pre>';
print_r( $contact);
echo '</pre>';
echo "\nTHE END";
因为您回复了之前的$json_contacts
,并且您没有启用PHP通知。您无法回复$contact
因为它是一个数组。