php 读取json数组
请教一下,如何用PHP读取JSON数组里面的值呢?现在想把A_Name第一个值a1值读取出来<?php$Tdata='[{"A_Name":"a1","ICNo":"",...
请教一下,如何用PHP读取 JSON数组里面的值呢?
现在想把 A_Name第一个值 a1 值读取出来
<?php
$Tdata='[{"A_Name":"a1","ICNo":"","A_ContNo":"","A_QQNo":""},{"A_Name":"a2","ICNo":"","A_ContNo":"","A_QQNo":""},{"A_Name":"a3","ICNo":"","A_ContNo":"","A_QQNo":""}]';
echo $Tdata . '<br/>';
$arr=json_decode($_POST['Tdata']);
echo (stripcslashes ($arr->A_Name[0]));
?> 展开
现在想把 A_Name第一个值 a1 值读取出来
<?php
$Tdata='[{"A_Name":"a1","ICNo":"","A_ContNo":"","A_QQNo":""},{"A_Name":"a2","ICNo":"","A_ContNo":"","A_QQNo":""},{"A_Name":"a3","ICNo":"","A_ContNo":"","A_QQNo":""}]';
echo $Tdata . '<br/>';
$arr=json_decode($_POST['Tdata']);
echo (stripcslashes ($arr->A_Name[0]));
?> 展开
5个回答
展开全部
<?php
$Tdata='[{"A_Name":"a1","ICNo":"","A_ContNo":"","A_QQNo":""},{"A_Name":"a2","ICNo":"","A_ContNo":"","A_QQNo":""},{"A_Name":"a3","ICNo":"","A_ContNo":"","A_QQNo":""}]';
$arr=json_decode($Tdata,true);
//var_dump($arr);
echo stripcslashes ($arr[0][A_Name]);
?>
上面的方法可以打印输出,json_decode($Tdata)输出的结果是一个对应,而json_decode($Tdata,true)输出的是一个数组
$Tdata='[{"A_Name":"a1","ICNo":"","A_ContNo":"","A_QQNo":""},{"A_Name":"a2","ICNo":"","A_ContNo":"","A_QQNo":""},{"A_Name":"a3","ICNo":"","A_ContNo":"","A_QQNo":""}]';
$arr=json_decode($Tdata,true);
//var_dump($arr);
echo stripcslashes ($arr[0][A_Name]);
?>
上面的方法可以打印输出,json_decode($Tdata)输出的结果是一个对应,而json_decode($Tdata,true)输出的是一个数组
追问
谢谢,请教一下, $arr[0][A_Name] 和 $arr[0]->A_Name 有什么区别呢?
我想读出 $num=count($A_Name); 就是A_Name在数组里面的数量多少,
还有怎么用 print_r把 A_Name 所有的都读出来呢?
追答
你那种方法没错,也可以用,这是转化为数组方便。
$arr[0][A_Name] 这个是数组,$arr[0]->A_Name这个是指针指向一个值的地址,你可以把json_decode($Tdata)输出的值打印出来,里面有三个对象。
而json_decode("$arr",true)是把它强制生成PHP关联数组,下面的方法可以得出里面的个数
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
代码应该是:
<?php
$Tdata='[{"A_Name":"a1","ICNo":"","A_ContNo":"","A_QQNo":""},{"A_Name":"a2","ICNo":"","A_ContNo":"","A_QQNo":""},{"A_Name":"a3","ICNo":"","A_ContNo":"","A_QQNo":""}]';
echo $Tdata . '<br/>';
$arr=json_decode($Tdata);
print_r($arr[0]->A_Name);
?>
<?php
$Tdata='[{"A_Name":"a1","ICNo":"","A_ContNo":"","A_QQNo":""},{"A_Name":"a2","ICNo":"","A_ContNo":"","A_QQNo":""},{"A_Name":"a3","ICNo":"","A_ContNo":"","A_QQNo":""}]';
echo $Tdata . '<br/>';
$arr=json_decode($Tdata);
print_r($arr[0]->A_Name);
?>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
json_decode($Tdata) 输出就行
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你问的是什么啊?这个程序没有问题啊
追问
可是读取不出来啊。我就想把数组里面的数据读取出来啊
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
$arr['A_Name']
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询