Php 检查json的计数
我有一些类似这样的json结果Php 检查json的计数,php,arrays,json,Php,Arrays,Json,我有一些类似这样的json结果 array ( 'status' => 'sukses', 'msg' => 'Resi Ada', 'gen_info' => array ( 'awb' => '030003437484', 'service' => '', 'date' => '21-07-2014 15:03:27', 'shipper' => '', 'shipper_city' =>
array (
'status' => 'sukses',
'msg' => 'Resi Ada',
'gen_info' =>
array (
'awb' => '030003437484',
'service' => '',
'date' => '21-07-2014 15:03:27',
'shipper' => '',
'shipper_city' => '',
'receiver' => '',
'receiver_city' => '',
'receiver_address' => '',
),
'manifest' =>
array (
0 =>
array (
'manifest_date' => '23-07-2014 17:00:00',
'manifest_desc' => 'Received By : DEDY SASMITA',
),
1 =>
array (
'manifest_date' => '23-07-2014 09:25:55',
'manifest_desc' => 'Ongoing',
),
2 =>
array (
'manifest_date' => '22-07-2014 18:10:08',
'manifest_desc' => 'ARRIVED AT DESTINATION',
),
3 =>
array (
'manifest_date' => '21-07-2014 18:14:56',
'manifest_desc' => 'Ongoing',
),
4 =>
array (
'manifest_date' => '21-07-2014 15:03:27',
'manifest_desc' => 'DEPART AT TIKI',
),
),
)
count of json is: 4
echo count($data['manifest']);
我想知道这个json的数量,使用这个脚本
echo count($data);
$data是解码后json的结果
但是,为什么得到的结果与json文件中的计数数不匹配
这样的结果
array (
'status' => 'sukses',
'msg' => 'Resi Ada',
'gen_info' =>
array (
'awb' => '030003437484',
'service' => '',
'date' => '21-07-2014 15:03:27',
'shipper' => '',
'shipper_city' => '',
'receiver' => '',
'receiver_city' => '',
'receiver_address' => '',
),
'manifest' =>
array (
0 =>
array (
'manifest_date' => '23-07-2014 17:00:00',
'manifest_desc' => 'Received By : DEDY SASMITA',
),
1 =>
array (
'manifest_date' => '23-07-2014 09:25:55',
'manifest_desc' => 'Ongoing',
),
2 =>
array (
'manifest_date' => '22-07-2014 18:10:08',
'manifest_desc' => 'ARRIVED AT DESTINATION',
),
3 =>
array (
'manifest_date' => '21-07-2014 18:14:56',
'manifest_desc' => 'Ongoing',
),
4 =>
array (
'manifest_date' => '21-07-2014 15:03:27',
'manifest_desc' => 'DEPART AT TIKI',
),
),
)
count of json is: 4
echo count($data['manifest']);
“4”是此json的计数
但是,总数应该是“5”,对吗
计数是0、1、2、3和4,所以数字是5
如何修复此问题?您正在检查外部数组,这就是为什么它将计数设为
4
,请使用下面的代码检查清单的计数,如下所示
array (
'status' => 'sukses',
'msg' => 'Resi Ada',
'gen_info' =>
array (
'awb' => '030003437484',
'service' => '',
'date' => '21-07-2014 15:03:27',
'shipper' => '',
'shipper_city' => '',
'receiver' => '',
'receiver_city' => '',
'receiver_address' => '',
),
'manifest' =>
array (
0 =>
array (
'manifest_date' => '23-07-2014 17:00:00',
'manifest_desc' => 'Received By : DEDY SASMITA',
),
1 =>
array (
'manifest_date' => '23-07-2014 09:25:55',
'manifest_desc' => 'Ongoing',
),
2 =>
array (
'manifest_date' => '22-07-2014 18:10:08',
'manifest_desc' => 'ARRIVED AT DESTINATION',
),
3 =>
array (
'manifest_date' => '21-07-2014 18:14:56',
'manifest_desc' => 'Ongoing',
),
4 =>
array (
'manifest_date' => '21-07-2014 15:03:27',
'manifest_desc' => 'DEPART AT TIKI',
),
),
)
count of json is: 4
echo count($data['manifest']);
或者,如果您有对象,则使用
echo count($data->manifest);
检查此演示:json的计数为4。其中只有四个要素:
状态、消息、gen_信息、清单
我想你是想清点舱单的数量。在这种情况下,只需使用
echo count($data->manifest) // If you have an object from json_decode
// Or
echo count($data['manifest']) // if you already converted it to an array.
它显示正确的输出,因为$data
有4个元素(从1开始)。
如果你想清点清单,你可以这样做
echo计数($data['manifest']);//输出5
,因为您正在计算根数组的条目(状态、消息、gen_信息和清单)。要获得您期望的内容,您需要执行count($data->manifest)
或count($data['manifest'))
,具体取决于您是正常解码还是对数组进行解码