php json_decode 如何输出数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6833737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
php json_decode how to output array
提问by genoki
Here is my php code with json formatted string:
这是我的带有 json 格式字符串的 php 代码:
<?php
$string='{"items": [
{
"address":"W 7th Ave"
},
{
"address":"W 8th St"
}
]}';
$json = json_decode($string, true);
foreach ($json as $key => $value){
echo "$key: $value\n";
};
?>
I want to learn how to parse/output json string into something I can show in html or put into database .. however i am stuck on something that is prob very simple but I've spent most of the morning trying to figure out.
我想学习如何将 json 字符串解析/输出为我可以在 html 中显示或放入数据库的内容。
What I want to understand is why the results of my code above gives me the following result:
我想了解的是为什么我上面的代码的结果给了我以下结果:
"items: Array"
"items: Array"
And not what I want/expect to get:
而不是我想要/期望得到的:
"items: W 7th Ave"
"items: W 8th St"
What am i missing? Isn't "Address" the next "level" down from "Item" in the array?
我错过了什么?“地址”不是数组中“项目”的下一个“级别”吗?
回答by Dalen
$string = file_get_contents('./string.json');
$json = json_decode($string);
if you want to have items: <address>
:
如果你想拥有items: <address>
:
foreach ($json['items'] as $address)
{
echo "items:". $address['address'] ."\n";
};
anyway if you are not sure about how an array is built you could print it via:
无论如何,如果您不确定数组是如何构建的,您可以通过以下方式打印它:
print_r($json);
which will print:
这将打印:
Array
(
[items] => Array
(
[0] => Array
(
[address] => W 7th Ave
)
[1] => Array
(
[address] => W 8th St
)
)
)
now you found out that $json
contains just an array (items
) of two array, then, if you loop it, you will get that array which is printed in your example.
As explained above you need to go one step deeper by looping the elements in your items
array and print their address
element.
现在您发现它只$json
包含items
两个数组的数组 ( ),然后,如果您循环它,您将得到在您的示例中打印的那个数组。如上所述,您需要更深入地循环items
数组中的元素并打印它们的address
元素。
here is the complete script: http://pastie.org/2275879
这是完整的脚本:http: //pastie.org/2275879
回答by TaylorOtwell
Your items arein an array. You could loop through them like this:
你的项目是在数组中。你可以像这样循环遍历它们:
foreach ($json['items'] as $address)
{
echo 'Address: '.$address;
}
回答by curtisp
BTW, I did do var_dump, print, print_r, switch it back and forth from Object to Array, to try to learn more about array structure etc and also did a bunch of variations of echo, and for and foreach loops, etc to try to get what i wanted from array.
顺便说一句,我确实做了 var_dump、print、print_r,将它从对象到数组来回切换,以尝试了解有关数组结构等的更多信息,并且还做了一堆 echo、for 和 foreach 循环等的变体,以尝试从数组中得到我想要的东西。
Ok so to summarize, the answers seem to indicate i have to:
好的,总结一下,答案似乎表明我必须:
- first get the whole array eg $string (did that)
- then decode array into $json (did that)
- then somehow parse out the sub arrays from $json (by doing something like referencing the addresses in array like "items.address" or "[items][address]" etc (i still am not sure from the answers above how to do this .. they hint at it but can't see syntax etc?)
- 首先获取整个数组,例如 $string (这样做)
- 然后将数组解码为 $json (这样做)
- 然后以某种方式从 $json 解析出子数组(通过执行诸如引用数组中的地址之类的操作,例如“items.address”或“[items][address]”等(我仍然不确定上面的答案如何做到这一点) .. 他们暗示它但看不到语法等?)
I tried both answers and got:
我尝试了两个答案并得到:
Using TaylorOtwell answer:
使用 TaylorOtwell 回答:
I got: Address: Array Address: Array
我得到:地址:数组地址:数组
Taylor
泰勒
Using Dalen's answer:
使用 Dalen 的回答:
I got: 0: Array 1: Array
我得到:0:数组 1:数组
Looks like i need to somehow loop through array a second time within the first foreach to get actual values?
看起来我需要在第一个 foreach 中以某种方式再次循环遍历数组以获取实际值?
Would it look something like this?
它看起来像这样吗?
foreach ($json['items'] as $key => $value)
{
foreach ($json['items']['address'] as $key => $value)
{
echo "$key: $value\n";
};
};
回答by Mehul S
First create a class for your elements.
首先为您的元素创建一个类。
function getAddress(address)
{
this.address=address;
}
Push objects to your array
将对象推送到您的数组
newAddress = JSON.stringify(new
getAddress(address));
AddressArray.push(newAddress);
Convert your array as JSON Array using
使用将您的数组转换为 JSON 数组
AllAddress=JSON.stringify(AddressArray);
Send your array to backend/fetch elements like
将您的数组发送到后端/获取元素,例如
$json_array = json_decode($AllAddress, true);
for($i=0;$i<count($json_array);$i++)
{
$eachAddress = json_decode($json_array[$i],true);
$address= $eachAddress["address"];//fetch particular element from your JSON Object
}
*Use implode(php) to get all elements from the array at once.
*使用 implode(php) 一次从数组中获取所有元素。
回答by Dimi
You might want to simplify your input string as follows
您可能希望按如下方式简化输入字符串
$string='{"address":["W 7th Ave","W 8th St"]}';
$string='{"address":["W 7th Ave","W 8th St"]}';
$json = json_decode($string, true);
echo'<pre>';
print_r($json);
echo'</pre>';
foreach ($json['address'] as $key=>$value){
echo "Address $key:". $value ."<br>";
};
$json = json_decode($string, true);
echo'<pre>';
print_r($json);
echo'</pre>';
foreach ($json['address'] as $key=>$value){
echo "Address $key:". $value ."<br>";
};