如何为每个json元素生成id no

时间:2015-05-08 03:59:47

标签: javascript php arrays json

我有一个json输出,如下所示。我想为每个节点提供增量id并进行解码。请帮帮我。

[{"message":"Test+sms","sender":"EXECUT","billcredit":"0.00","messageStatus":"DND","sendondate":"2015-04-22 15:22:00","provider":"AIRTEL"},{"message":"Test+sms","sender":"EXECUT","billcredit":"0.00","messageStatus":"DND","sendondate":"2015-04-22 15:22:00","provider":"AIRTEL"},{"message":"Test+sms","sender":"EXECUT","billcredit":"1.00","messageStatus":"DELIVRD","sendondate":"2015-04-22 15:22:00","provider":"AIRTEL"},{"message":"Test+sms","sender":"EXECUT","billcredit":"1.00","messageStatus":"DND REJECTED","sendondate":"2015-04-22 15:22:00","provider":"VODAFONE"}]

我想输出

id:1
messageStatus:DND
id:2
messageStatus:DND
id:3
messageStatus:DELIVERD
id:4
messageStatus:DND

1 个答案:

答案 0 :(得分:0)

$json = '[{"message":"Test+sms","sender":"EXECUT","billcredit":"0.00","messageStatus":"DND","sendondate":"2015-04-22 15:22:00","provider":"AIRTEL"},{"message":"Test+sms","sender":"EXECUT","billcredit":"0.00","messageStatus":"DND","sendondate":"2015-04-22 15:22:00","provider":"AIRTEL"},{"message":"Test+sms","sender":"EXECUT","billcredit":"1.00","messageStatus":"DELIVRD","sendondate":"2015-04-22 15:22:00","provider":"AIRTEL"},{"message":"Test+sms","sender":"EXECUT","billcredit":"1.00","messageStatus":"DND REJECTED","sendondate":"2015-04-22 15:22:00","provider":"VODAFONE"}]
';

var_dump(json_decode($json));

在这里,您将获得每个对象的ID。我不能将其作为评论发布,因为它太长了。