2

I want to fetch API from https://www.travel-advisory.info/api then I already write my code

  $curl = new CurlService();
  $response = $curl->to('https://www.travel-advisory.info/api')->get();
  throw_if(!$response, Exception::class, 'Terjadi kesalahan: Data tidak dapat diperoleh');      
  $data = json_decode($response);
  echo $data->data;

so this is example response from API

  "data": {
    "AD": {
      "iso_alpha2": "AD",
      "name": "Andorra",
      "continent": "EU",
      "advisory": {
        "score": 2.79999999999999982236431605997495353221893310546875,
        "sources_active": 4,
        "message": "",
        "updated": "2020-08-04 07:21:19",
        "source": "https://www.travel-advisory.info/andorra"
      }
    },
    "AE": {
      "iso_alpha2": "AE",
      "name": "United Arab Emirates",
      "continent": "AS",
      "advisory": {
        "score": 3.100000000000000088817841970012523233890533447265625,
        "sources_active": 7,
        "message": "",
        "updated": "2020-08-04 07:21:19",
        "source": "https://www.travel-advisory.info/united-arab-emirates"
      }
    },
    "AF": {
      "iso_alpha2": "AF",
      "name": "Afghanistan",
      "continent": "AS",
      "advisory": {
        "score": 5,
        "sources_active": 10,
        "message": "",
        "updated": "2020-08-04 07:21:19",
        "source": "https://www.travel-advisory.info/afghanistan"
      }
    },
    "AG": {
      "iso_alpha2": "AG",
      "name": "Antigua and Barbuda",
      "continent": "NA",
      "advisory": {
        "score": 3,
        "sources_active": 3,
        "message": "",
        "updated": "2020-08-04 07:21:19",
        "source": "https://www.travel-advisory.info/antigua-and-barbuda"
      }
    },
    "AI": {
      "iso_alpha2": "AI",
      "name": "Anguilla",
      "continent": "NA",
      "advisory": {
        "score": 3,
        "sources_active": 3,
        "message": "",
        "updated": "2020-08-04 07:21:19",
        "source": "https://www.travel-advisory.info/anguilla"
      }
    }
  }

my question is how to filter continent : "AS" ? p

2
  • 1
    use foreach and access the object Commented Aug 4, 2020 at 15:38
  • The string is in JSON format, so first convert that into assoc array: $ar = json_decode($your_result, true); then iterate through this array as mentioned above, using foreach. Commented Aug 4, 2020 at 15:55

3 Answers 3

1
$data = json_decode($response, true)['data'];
print_r($this->continentFilter($data, 'AS'));

The function will be like:

private function continentFilter(array $data, string $continent): array
{
    $filteredArray = [];
    foreach ($data as $item) {
        if ($item['continent'] === $continent) {
            $filteredArray = $item;

            break;
        }
    }

    return $filteredArray;
}
Sign up to request clarification or add additional context in comments.

Comments

0

you can use json_decode with array flag and array_filter method like this:

$data = (json_decode($response, true))['data'];

$as = array_filter($data, function($item) {
    return $item['continent'] === 'AS';
});

Also please note, that the json you give as example above needs to wrapped in curly braces all together, like this:

{
  "data": {
  ...
}

Comments

0

You can loop through each object and push to a new array, if the continent value matches "AS".

Using $key => $value allows you to retain the indexes.

Example:

$filtered = [];
foreach ( $data->data as $key => $value )
{
    if ( $value->continent === "AS" )
    {
        $filtered[$key] = $value;
    }
}

print_r($filtered);

Will give you:

[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
            )

    )
    ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.