How to convert json data into array string in php

How to convert json data into array string in php

First of all you need to know about Api. Now, what is api ?? the answer is an Api stands for (Application program interface). It is used to get information and data from other places and use indifferent application using one Api. So, let’s covert Json data of api into an array. We have on api of corona virus which have data in json format.

Api : "https://api.covid19india.org/data.json"


By using above api link we will get data into an array format. We are getting the data using curl method in php programming laugauge. The below code is a curl api code to get the data from api. Please follow the below steps for convert json data in to array string.


1.    Insert your api in curl code

$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.covid19india.org/data.json",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
));

This above code is curl code which have different attributes. All the value of this attribute is set by default. If you want to change it and add new attribute along with some value you can also do it.


2.    Execute query to get data from api using below code

$response = curl_exec($curl);

We can execute quary by taking one variable so we can store data in to that particular variable as well.


3.    Close connection

curl_close($curl);

After executing query, we need to close connection which having with api. So, follow the above code to close connection.


4.    Get data in a variable

echo $response;

As I mention above, we stored data in variable called “$response” now we need to print that data using “echo” so we can get idea of actual result is coming or not.

5.    Convert json data into array

$response=json_decode($response);
We need to decode Jason data by using above method to convert json data into an array string. By executing above statement, we got all data in to one array string.

That’s all. Now you got all data into array. If you find this article is helpful don’t forget to share with your friends. Please help us by doing comment using comment box. Thank you!

Also Read:

Sushant Singh Rajput commits suicide in his Bandra home

Post Comments(0)

Leave a reply