I am trying to add values from a JSON API to a MySQL. The problem is that the result return without the [], so I cannot foreach it and insert all the values to the database.
This is the API URL
This is the code I am having
$data = file_get_contents(
"https://horoscope-api.herokuapp.com/horoscope/today/Gemini",
true);
$array = json_decode($data, true); //Convert JSON String into PHP Array
foreach($array as $row) //Extract the Array Values by using Foreach Loop
{
$query .= "INSERT INTO zodiac(date, horoscope, sunsign, url)
VALUES (
'" . $row["date"]. "',
'" . $row["horoscope"]."',
'" . $row["sunsign"]."',
'" . $row["sunsign"]."'
); "; // Make Multiple Insert Query
}
If I combine $data = "[$data]"; (with and without the quotes) it prints correctly but cannot be parsed as an array. Or at least I cont know how to do it. I have tried searching for some way to fix that and found some interesting ideas here How to convert JSON string to array but none of them seems to work.
And other way to get the result within []?
Thanks