3 回答

TA貢獻(xiàn)1812條經(jīng)驗(yàn) 獲得超5個贊
$data = json_decode($response, true)['data'];
print_r($this->continentFilter($data, 'AS'));
該函數(shù)將類似于:
private function continentFilter(array $data, string $continent): array
{
$filteredArray = [];
foreach ($data as $item) {
if ($item['continent'] === $continent) {
$filteredArray = $item;
break;
}
}
return $filteredArray;
}

TA貢獻(xiàn)1811條經(jīng)驗(yàn) 獲得超6個贊
您可以使用json_decode數(shù)組標(biāo)志和array_filter方法,如下所示:
$data = (json_decode($response, true))['data'];
$as = array_filter($data, function($item) {
return $item['continent'] === 'AS';
});
另請注意,上面示例中給出的 json 需要全部用大括號括起來,如下所示:
{
"data": {
...
}

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超5個贊
如果大陸值與“AS”匹配,您可以循環(huán)遍歷每個對象并推送到新數(shù)組。
使用$key => $value允許您保留索引。
例子:
$filtered = [];
foreach ( $data->data as $key => $value )
{
if ( $value->continent === "AS" )
{
$filtered[$key] = $value;
}
}
print_r($filtered);
會給你:
[AE] => stdClass Object
(
[iso_alpha2] => AE
[name] => United Arab Emirates
[continent] => AS
[advisory] => stdClass Object
(
[score] => 3.1
[sources_active] => 7
[message] =>
[updated] => 2020-08-04 07:21:19
[source] => https://www.travel-advisory.info/united-arab-emirates
)
)
[AF] => stdClass Object
(
[iso_alpha2] => AF
[name] => Afghanistan
[continent] => AS
[advisory] => stdClass Object
(
[score] => 5
[sources_active] => 10
[message] =>
[updated] => 2020-08-04 07:21:19
[source] => https://www.travel-advisory.info/afghanistan
)
)
...
- 3 回答
- 0 關(guān)注
- 169 瀏覽
添加回答
舉報