简体   繁体   English

如何从下至上访问和打印Json数据

[英]How to access & Print Json data from Bottom to Top

I have a json data like 我有一个JSON数据像

{"Nepal":"1","India":"3","Pakistan":"2","America":"25","Bangaladesh":"23"}

Now what I want is to print and access data from last to top to print like this using PHP 现在我想要的是使用PHP从头到尾打印和访问数据以进行打印

Bangaladesh 23
America 25
Pakistan 2
India 3
Nepal 1

Thank you in advance.. 先感谢您..

you can do reverse loop like this using list.length . 您可以使用list.length进行这样的反向循环。 Code depends upon the language you are using. 代码取决于您使用的语言。

for(var i=list.length-1;i>=0;i--){
 //your printing here
}

As Ruchan mentioned , iterating over your JSON Array object in reverse is a solution. Ruchan所述 ,反向迭代JSON Array对象是一种解决方案。 Here is a snippet for PHP 这是PHP的代码段

//Decode the JSON
$array = json_decode('[{"Nepal":"1","India":"3","Pakistan":"2","America":"25","Bangaladesh":"23"}]',false); //Note: Second parameter is a boolean indicating if the resulting array should be associative or not

//Get the length of the Array
$length = count($array);

//Iterate over the array from the last index to the first (ie, length-1 to 0)
while($length) {
    echo sprintf("<p>%s</p>", $array[--$length]);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM