2017-04-22 68 views
1

如何在一個循環中獲得相同的結果/值而不是兩個?從php循環獲取json值

$json = file_get_contents($url) 
$data = json_decode($json, true); 


$desc = $data["descriptions"]; 
$assets = $data["assets"]; 

foreach ($assets as $assItem) { 
    echo $assItem["assetid"]; 
} 


foreach($desc as $descItem) { 
    echo descItem["name"]; 
} 

我已經試過類似

$json = file_get_contents($url); 
$data = json_decode($json, true); 

foreach ($data as $item) { 
    echo $item["assets"]["assetid"]; 
    echo $item["descriptions"]["name"]; 
} 

引擎收錄到JSON:https://pastebin.com/raw/uA9mvE2e

+0

我刪除了我的答案,因爲問題並不清楚。粘貼'var_dump($ data)'和期望的輸出。這樣我們可以更好地幫助你。 – Jigar

+0

發佈json文件格式 – julekgwa

+0

添加pastebin到主帖子中的json – user3187651

回答

1

你可以這樣做:

$json = file_get_contents($url); 
$data = json_decode($json, true); 

foreach ($data['assets'] as $k => $item) { 
    echo $item["assetid"]; 
    echo $data["descriptions"][$k]["name"]; 
} 

這假定$data['assets']$data['descriptions']份額相同的指數。

+1

作品就像一個魅力!乾杯:D – user3187651