我在使用PHP时遇到了json数组数据的问题。我使用foreach
循环数据,只捕获第一个[标签]列表。未捕获数组索引1和更大的所有内容。
在包含的json中,$keywordfilters
将回显:
“领导力,新闻稿”将不会从索引1中删除。
请帮忙。
PHP:
// Keyword Filters
echo '<div class="sub-nav" role="menu">';
// Loop to get all keywords for filtering
$recordcounter = 0;
foreach($json->items as $jsondata){
$keywordfilters = '<strong style="margin: 0 3px 3px; padding: 3px 10px 3px 0; display: block;">Filter:</strong> <a href="#" role="menuitem" class="active" style="margin: 0 3px 3px; padding: 3px 10px 3px 0; display: block; float: left;">All</a>';
$keywordcounter = 0;
foreach($jsondata->labels as $keywordlabels){
//foreach($keywordlabels as $labels){
//strtolower,strtoupper,ucfirst,lcfirst
$keywordfilters .= '<a href="#" role="menuitem" style="margin: 0 3px 3px; padding: 3px 10px 3px 0; display: block; float: left;">' . ucfirst($keywordlabels) . '</a>';
$keywordcounter++;
//}
}
$recordcounter++;
}
echo $keywordfilters;
echo '</div>';
JSON:
stdClass Object
(
[kind] => blogger#postList
[nextPageToken] => CgkIChiA48Lo_igQg4DLkK7p2Kke
[items] => Array
(
[0] => stdClass Object
(
[kind] => blogger#post
[id] => a123456789
[content] => TESTESTEST
[author] => stdClass Object
(
[id] => sro5t7y4nte76vtcmhsazsdffsdy
[displayName] => MyDisplayName
[url] => https://www.blogger.com/profile/a1234567890
[image] => stdClass Object
(
[url] => //lh6.googleusercontent.com/-3HzpopblNls/AAAAAAAAAAI/AAAAAAAADC8/YJsx2U5TQ9I/s35-c/photo.jpg
)
)
[labels] => Array
(
[0] => Google
[1] => intelligence
[2] => press release
[3] => leadership
)
)
[1] => stdClass Object
(
[kind] => blogger#post
[id] => b123456789
[content] => TESTESTEST
[author] => stdClass Object
(
[id] => sro5t7y4nte76vtcmhsazsdffsdy
[displayName] => MyDisplayName
[url] => https://www.blogger.com/profile/a1234567890
[image] => stdClass Object
(
[url] => //lh6.googleusercontent.com/-3Hz321blBls/AAABBBABACAAAI/AACCC76AADC8/YJsx2U5TQ9I/s35-c/photo.jpg
)
)
[labels] => Array
(
[0] => leadership
[1] => press release
)
)
)
[etag] => "rone8ryudb8ditsvmp8re8dt/oprdnyt75GRFYTUiofygjlhft8y"
)
答案 0 :(得分:1)
在每个外环中: $ keywordfilters正在重置。
$keywordfilters = '<strong style="margin: 0 3px 3px; padding: 3px
10px 3px 0; display: block;">Filter:</strong> <a href="#"
role="menuitem" class="active" style="margin: 0 3px 3px; padding:
3px 10px 3px 0; display: block; float: left;">All</a>';
您需要使用。=来追加以前数组的数据。
$keywordfilters .= '<strong style...
答案 1 :(得分:1)
试试这个,希望这会帮助你。 问题是这个
$keywordfilters = '<strong style="margin: 0 3px 3px; padding: 3px 10px 3px 0; display: block;">Filter:</strong> <a href="#" role="menuitem" class="active" style="margin: 0 3px 3px; padding: 3px 10px 3px 0; display: block; float: left;">All</a>';
您在foreach
循环中使用此语句,每次都会覆盖它。
PHP代码:
<?php
ini_set('display_errors', 1);
$string = '{ "kind" :"blogger#postList", "nextPageToken" :"CgkIChiA48Lo_igQg4DLkK7p2Kke", "items" : { "0" : { "kind" :"blogger#post", "id" :"a123456789", "content" :"TESTESTEST", "author" : { "id" :"sro5t7y4nte76vtcmhsazsdffsdy", "displayName" :"MyDisplayName", "url" :"https://www.blogger.com/profile/a1234567890", "image" : { "url" :"//lh6.googleusercontent.com/-3HzpopblNls/AAAAAAAAAAI/AAAAAAAADC8/YJsx2U5TQ9I/s35-c/photo.jpg"}}, "labels" : { "0" :"Google", "1" :"intelligence", "2" :"press release", "3" :"leadership"}}, "1" : { "kind" :"blogger#post", "id" :"b123456789", "content" :"TESTESTEST", "author" : { "id" :"sro5t7y4nte76vtcmhsazsdffsdy", "displayName" :"MyDisplayName", "url" :"https://www.blogger.com/profile/a1234567890", "image" : { "url" :"//lh6.googleusercontent.com/-3Hz321blBls/AAABBBABACAAAI/AACCC76AADC8/YJsx2U5TQ9I/s35-c/photo.jpg"}}, "labels" : { "0" :"leadership", "1" :"press release"}}}, "etag" :"rone8ryudb8ditsvmp8re8dt/oprdnyt75GRFYTUiofygjlhft8y" }';
$json=json_decode($string);
echo '<div class="sub-nav" role="menu">';
// Loop to get all keywords for filtering
$recordcounter = 0;
$keywordfilters = '<strong style="margin: 0 3px 3px; padding: 3px 10px 3px 0; display: block;">Filter:</strong> <a href="#" role="menuitem" class="active" style="margin: 0 3px 3px; padding: 3px 10px 3px 0; display: block; float: left;">All</a>';
foreach($json->items as $jsondata){
$keywordcounter = 0;
foreach($jsondata->labels as $keywordlabels){
//foreach($keywordlabels as $labels){
//strtolower,strtoupper,ucfirst,lcfirst
$keywordfilters .= '<a href="#" role="menuitem" style="margin: 0 3px 3px; padding: 3px 10px 3px 0; display: block; float: left;">' . ucfirst($keywordlabels) . '</a>';
$keywordcounter++;
//}
}
$recordcounter++;
}
echo $keywordfilters;