0

I have REST API and looks like below :

{
"rajaongkir": {
    "query": {
        "key": "b5231ee43b8ee75764bd6a289c4c5745"
    },
    "status": {
        "code": 200,
        "description": "OK"
    },
    "results": [
        {
            "city_id": "1",
            "province_id": "21",
            "province": "Nanggroe Aceh Darussalam (NAD)",
            "type": "Kabupaten",
            "city_name": "Aceh Barat",
            "postal_code": "23681"
        },
        {
            "city_id": "2",
            "province_id": "21",
            "province": "Nanggroe Aceh Darussalam (NAD)",
            "type": "Kabupaten",
            "city_name": "Aceh Barat Daya",
            "postal_code": "23764"
        }
     ]
  }
}

I want to consume this API. I call the object like code below :

<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">

<title>Raja Ongkir</title>
</head>
<body>
  <h1>Raja Ongkir</h1>
  <?php 
    echo var_dump($data->rajaongkir->results[0]); 
  ?>
</body>
</html>

If I call first element of JSON using this line

echo var_dump($data->rajaongkir->results[0]);

OR

echo var_dump($data->rajaongkir->results[0]->city_name);

I got the output that I want. But If I try to get all city_id or city_name in results object, using this code

echo var_dump($data->rajaongkir->results->city_name);

I got this error

Message: Trying to get property 'city_name' of non-object

How to fix it?

2 Answers 2

1

You can use loop for all city name. For example :

foreach ($data->rajaongkir->results as $v) {
   echo $v->city_name."\n";
}
Sign up to request clarification or add additional context in comments.

2 Comments

How can I get just first element of JSON? I try $v->city_name[0] I got error.
if you use loop it run for each element in results. if you only want to reach city_name in first object u can try this. echo $data->rajaongkir->results[0]->city_name;
1

Try the following instead:

$all_city_names = array_column(json_decode($data->rajaongkir->results, true), 
                               'city_name');

// Display all the city name(s)
var_dump($all_city_names);

Details:

  • json_decode(), with second parameter set to true, will convert the JSON to array [of arrays].
  • array_column() function can be used to extract a one-dimensional array of a particular key values, out of a two-dimensional array.

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.