I am trying to send a php array to jquery
This is my array before json encode:
Array ( [0] => 30-Dec [1] => 31-Dec [2] => 1-Jan [3] => 2-Jan [4] => 3-Jan [5] => 4-Jan [6] => 5-Jan [7] => 6-Jan )
This is it after the json encode:
["30-Dec","31-Dec","1-Jan","2-Jan","3-Jan","4-Jan","5-Jan","6-Jan"]
If I make a variable in jquery with the json encoded array and print this in the console I got the following:
[" ;30-Dec" ;,"31-Dec" ;," ;1-Jan" ;," ;2-Jan" ;," ;3-Jan" ;," ;4-Jan" ;," ;5-Jan" ;," ;6-Jan" ;]
But then without the spaces between the t
and ;
I am using twig and silex for my websites.
EDIT
This is my code PHP code
function getDates($startTime, $endTime) { $day = 86400;
$format = 'j-M';
$startTime = strtotime($startTime);
$endTime = strtotime($endTime);
$numDays = round(($endTime - $startTime) / $day) + 1;
$days = array();
for ($i = 0; $i < $numDays; $i++) {
$days[] = date($format, ($startTime + ($i * $day)));
}
return $days;
}
$days = getDates($lastday, $today);
$days = json_encode($days);
This is my twig/jquery code
var days = '{{ days }}';
console.log(days);
json_encode
, far more likely a result of running it through templating engines. Read the manual docs forjson_encode