[英]PHP foreach on nested array
I am doing a GET php function on a URL and I get back the resulting response in JSON:我正在对 URL 执行 GET php 函数,然后以 JSON 形式返回结果响应:
{
"results": [{
"text": "The 2014 BICSI Canadian Conference and Exhibition in Vancouver, British Columbia, Canada is 61 days away on April 27 - 30.",
"createdAt": "2014-02- 24T19:54:08.707Z",
"updatedAt": "2014-02-24T19:54:08.707Z",
"objectId": "ZZrZ9OgyRG"
}, {
"text": "Only 33 more days fro the 2014 BICSI Canadian Conference and Exhibition in Vancouver, Canada!",
"createdAt": "2014-03-24T13:23:56.240Z",
"updatedAt": "2014-03-24T13:23:56.240Z",
"objectId": "ZqxJRiHoJo"
}]
}
I am able to do php json_decode
and display the results below on a web page like below我能够执行 php
json_decode
并在如下所示的网页上显示以下结果
text | The 2014 BICSI Canadian Conference and Exhibition in Vancouver, British Columbia, Canada is 61 days away on April 27 - 30.
createdAt | 2014-02-24T19:54:08.707Z
updatedAt | 2014-02-24T19:54:08.707Z
objectId | ZZrZ9OgyRG
text | Only 33 more days fro the 2014 BICSI Canadian Conference and Exhibition in Vancouver, Canada!
createdAt | 2014-03-24T13:23:56.240Z
updatedAt | 2014-03-24T13:23:56.240Z
objectId | ZqxJRiHoJo
The php code I used to display the results above on the web page is:我用来在网页上显示上述结果的php代码是:
$returned_content = get_data('https://api.parse.com/1/classes/Alerts');
$data = json_decode($returned_content, true);
foreach ($data as $array1 => $arrayn) {
foreach ($arrayn as $k => $v) {
foreach ($v as $t => $s) {
echo"<p> $t | $s ";
}
}
}
If I just wanted to display the 'text' key/value info only on the web page, how should I modify the php.如果我只想在网页上显示“文本”键/值信息,我应该如何修改 php.ini 文件? Meaning all I want to see displayed on the web page is:
这意味着我想在网页上看到的所有内容是:
text | The 2014 BICSI Canadian Conference and Exhibition in Vancouver, British Columbia, Canada is 61 days away on April 27 – 30.
text | Only 33 more days fro the 2014 BICSI Canadian Conference and Exhibition in Vancouver, Canada!
$data = json_decode($json);
foreach ($data->results as $item) {
echo '<br>text | '.$item->text;
}
If you don't add the second json_decode()
paremeter, you can just use your JSON with the StdClass.如果不添加第二个
json_decode()
参数,则可以将 JSON 与 StdClass 一起使用。
Wrap it inside a simple if condition.将它包装在一个简单的 if 条件中。
foreach ($data as $array1 => $arrayn) {
foreach($arrayn as $k => $v) {
foreach ($v as $t => $s) {
if($t == 'text') {
echo"<p> $t | $s ";
}
}
}
}
do like this这样做
$returned_content = get_data('https://api.parse.com/1/classes/Alerts');
$data = json_decode($returned_content, true);
foreach ($data as $array1 => $arrayn) {
foreach($arrayn as $k => $v){
foreach ($v as $t => $s)
{
if($t == "text")
echo"<p> $t | $s ";
}
}
}
After this line :在这一行之后:
$data = json_decode($returned_content, true);
You have an associative array, so you can change your loop to :您有一个关联数组,因此您可以将循环更改为:
foreach ($data as $array1 => $arrayn) {
foreach($arrayn as $k => $v) {
foreach ($v as $t => $s) {
if('text' === $t) {
echo"<p> $t | $s ";
}
}
}
}
Make it simple and with a check if the key exists, just in case the JSON does not contain the key "text" it will return notice.让它变得简单并检查密钥是否存在,以防万一 JSON 不包含密钥“文本”,它将返回通知。
$data = json_decode($str,true);
foreach($data["results"] as $key=>$val){
if(array_key_exists("text",$val)){
echo 'text | '.$val["text"];
echo '<br />';
}
}
Here $str
is your json string.这里
$str
是你的 json 字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.