I wonder if someone could avise where I am going wrong.
Using PHP the following code is being used successfully to fetched the JSON array from an external source:
$jsonData = file_get_contents($link);
… following which the retrieved data appears to have also been successfully re-built and converted into a PHP array using the following code:
array_push($AllTable1, array(
"bookingid" => $bookingid2,
"dayname" => $dayname6,
"daydate" => $daydate4,
"monthname" => $monthname,
"starttime" => $starttime4,
"title" => $title7,
"room" => $room7,
"roomcol" => $roomcol,
"roomcol2" => $roomcol2
));
When the following code is run:
$AllTable1 = str_replace(PHP_EOL, '', $AllTable1);
echo json_encode($AllTable1, JSON_PRETTY_PRINT);
… data in the following format is successfully returned without any associated errors:
0
bookingid "window.open('https://<pathtobooking>')"
dayname "Mon"
daydate "04"
monthname "December"
starttime "10:30"
title "Yoga Classes"
room "Large Hall"
roomcol "#b30c35"
roomcol2 ""
1
bookingid "window.open('https://<pathtobooking>')"
dayname "Mon"
daydate "04"
monthname "December"
starttime "19:00"
title "History Group"
room "Small Hall"
roomcol "#428bca"
roomcol2 ""
2
bookingid "window.open('https://<pathtobooking>')"
dayname "Tue"
daydate "05"
monthname "December"
starttime "10:00"
title "All Welcome Coffee Morning"
room "Kitchen,Small Hall"
roomcol "#41753f"
roomcol2 "#428bca"
3
bookingid "window.open('https://<pathtobooking>')"
dayname "Tue"
daydate "05"
monthname "December"
starttime "20:30"
title "Table Tennis Club"
room "Large Hall"
roomcol "#b30c35"
roomcol2 ""
4
... etc,etc
However, when the following code is run:
print_r($AllTable1);
… the following error is returned along with the array data:
SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
Array
(
[0] => Array
(
[bookingid] => window.open('https://<pathtobooking>')
[dayname] => Mon
[daydate] => 04
[monthname] => December
[starttime] => 10:30
[title] => Yoga Classes
[room] => Large Hall
[roomcol] => #b30c35
[roomcol2] =>
)
[1] => Array
(
[bookingid] => window.open('https://<pathtobooking>')
[dayname] => Mon
[daydate] => 04
[monthname] => December
[starttime] => 19:00
[title] => History Group
[room] => Small Hall
[roomcol] => #428bca
[roomcol2] =>
)
[2] => Array
(
[bookingid] => window.open('https://<pathtobooking>')
[dayname] => Tue
[daydate] => 05
[monthname] => December
[starttime] => 10:00
[title] => All Welcome Coffee Morning
[room] => Kitchen,Small Hall
[roomcol] => #41753f
[roomcol2] => #428bca
)
[3] => Array
(
[bookingid] => window.open('https://<pathtobooking>')
[dayname] => Tue
[daydate] => 05
[monthname] => December
[starttime] => 20:30
[title] => Table Tennis Club
[room] => Large Hall
[roomcol] => #b30c35
[roomcol2] =>
)
[4] => Array
... etc,etc
Also, wheneer the following code is run to attempt to output data from the array, for example:
$ddata = json_encode($AllTable1, true);
echo("The day of the sixth booking in the list is: n");
echo $ddata[5]['dayname'];
… the following error and output is returned (There are more than six sets of data in the array, btw.):
SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
The day of the sixth booking in the list is:
Effectively, the error remains and no data is returned.
Is it possibly that the data is not correctly formatted? If so, I cannot see why as the array data appears to be returned correctly in both the above array examples provided, despite the JSON error.
I’d be very grateful if anyone could assist and advise what is being done wrong and what needs to be done to resolve the issue.