【问题标题】:Retrieving data from a countries API从国家 API 检索数据
【发布时间】:2021-12-07 08:22:37
【问题描述】:

我正在使用 API 来获取 Laravel 中的国家/地区列表。我只能隔离国家列表,但国家现在有一个包含国家详细信息的内部数组。我只需要名字。我试过使用foreach,但没有用。 以下是 api 的结果

#items: array:2 [▼
"countries" => array:198 [▼
  0 => array:4 [▼
    "code" => "BT"
    "name" => "Bhutan"
    "geonameid" => 1252634
    "depends_on" => null
  ]
  1 => array:4 [▶]

下面是获取它的代码

$response = Http::withHeaders([
        'x-rapidapi-host' => 'world-geo-data.p.rapidapi.com',
        'x-rapidapi-key' => '1fa601995fmshcc1d2fcc97ee24cp1ac29djsn9b9b11cc10ec',
        //'x-rapidapi-key' => config('app.rapid_api_key'),
    ])->get('https://world-geo-data.p.rapidapi.com/countries');
    
    //dd($response->collect());
    
    return $response->collect()->pluck('name')
    ->all();

我试图只获取名称值。返回是返回 null

【问题讨论】:

  • 可以分享代码吗?
  • @IrshadKhan 代码共享

标签: laravel laravel-8


【解决方案1】:

我想这就是你想要的:

return collect($response->collect()["countries"])->pluck('name');

结果会是这样的:

【讨论】:

  • 没错!谢谢。你
猜你喜欢
  • 2019-04-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-23
  • 2016-09-20
  • 1970-01-01
相关资源
最近更新 更多