多维数组从json获取一个特定的项目



我已经在这方面工作了很长时间,我不能找到一个解决方案,任何帮助将不胜感激

这是我的JSON文件:
{   "city": "Drf",
    "tels": {
    "1": {
        "name": "Hilorf",
        "adr": "eldo",
        "partners": {
            "101": {
                "name": "xyz.com",
                 "prices": {
                    "1001": {
                        "description": "Single Room",
                        "amount": 125
                    },
                    "1002": {
                        "description": "Double Room",
                        "amount": 139
                    }
                }
            }
        }
    },
    "2": {
        "name": "Mer",
        "adr": "lichef",
        "partners": {
            "201": {
                "name": "vg.com",
                "prices": {
                    "2001": {
                        "description": "Single Room",
                        "amount": 52
                    },
                    "2002": {
                        "description": "Double Room",
                        "amount": 52
                    }
                }
            }
        }
    }
}

}

这是我的php文件
 <?php
$json_file = file_get_contents('xyz.json');
$jfo = json_decode($json_file, TRUE);
foreach("tried a lot here nothing seems to work fo me")

这里我想在一个数组中获取所有的amount值。我看不懂,请帮帮我。

您需要多个foreach()语句来完成此操作,每个级别一个。

$amounts = [];
foreach ($jfo as $people) {
    foreach ($people as $person) {
        foreach ($person['partners'] as $partner) {
            foreach ($partner['prices'] as $price) {
                $amounts[] = $price['amount'];
            }
        }
    }
}
print_r($amounts);
这个输出:

Array
(
    [0] => 125
    [1] => 139
    [2] => 52
    [3] => 72
)

相关内容

  • 没有找到相关文章

最新更新