[英]I have a json file with a list of associative arrays and I need to get it into a php indexed array that I can pull values out of
我無法弄清楚如何從 json 文件中獲取關聯數組列表並將其轉換為關聯數組的 php 索引數組,然后訪問嵌套在關聯數組中的值
這是json文件
[
{
"homeTeam":"team1",
"homeScore":"00",
"awayTeam":"team2",
"awayScore":"00",
"gameProgression": "Not Started"
},
{
"homeTeam":"team1",
"homeScore":"00",
"awayTeam":"team2",
"awayScore":"00",
"gameProgression": "Not Started"
}
]
以及我一直在嘗試使用的 php 代碼
$gameFile = file_get_contents("currentGames.json");
$gameFileReady = json_decode($gameFile,true);
$gameArray = array_keys($gameFileReady);
if ($gameArray[0]['gameProgression'] != "Not Started") {
--code--
};
提前致謝
在這段代碼中, array_keys
只會給你類似Array ( [0] => 0 [1] => 1 )
。 所以沒有必要實現你想要的。
只需刪除該行並執行以下操作:
$gameFile = file_get_contents("currentGames.json");
$gameArray = json_decode($gameFile,true);
if (!isset($gameArray[0]['gameProgression'])) {
die('an error occurred');
}
if ($gameArray[0]['gameProgression'] != "Not Started") {
echo 'something';
};
isset 只是為了很好的衡量,但不會改變您訪問數組項的方式。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.