我試圖訪問一個JSON請求的各個部分,一個典型的反應是:訪問JSON值
{
"Overview" : {
"firstresultposition" : 1,
"totalresultsreturned" : 2,
"linkpop" : "2",
"domainpop" : "2",
"netpop" : "1",
"ippop" : "1"
},
"Results" : [
{
"Links" : [
{
"UrlTo" : "http://www.eclecticaartisans.com/",
"Anchor" : "Eclectica Artisans' Shop",
"nofollow" : "0"
}
],
"IP" : "64.14.78.39",
"Index" : 1,
"DomainRank" : "5",
"UrlFrom" : "http://www.shoppernews.com/directory.html"
},
{
"UrlFrom" : "http://shoppernews.com/directory.html",
"DomainRank" : "4",
"Index" : 2,
"IP" : "64.14.78.39",
"Links" : [
{
"UrlTo" : "http://www.eclecticaartisans.com/",
"Anchor" : "Eclectica Artisans' Shop",
"nofollow" : "0"
}
]
}
]
}
我想要做的就是2個數據在1分的foreach循環而不是2本:
<?php
$returnSK = file_get_contents("REQUEST_URL");
$seoKicks = json_decode($returnSK, true);
print "<pre>"; print_r($returnSK); print "</pre>";
foreach ($seoKicks['Results'] as $key => $val2) {
echo $val2['UrlFrom'];
foreach ($val2['Links'] as $arr) {
echo $arr['Anchor'];
}
}
?>
上面的代碼工作正常,但我真的需要訪問「UrlFrom」,並使用1個foreach循環「錨」值(如果可能的2層的foreach的上面稍微當添加到數據庫中時,將其放入我的實時項目中會很複雜),我們將不勝感激。
什麼是你想用鏈接數據呢?如果一直只有一個,你可以通過索引訪問它,而不需要循環,否則你需要迭代或映射Links數組。 – charlietfl