I have this PHP array ($savedRequestDates
) of dates:
Array ( [0] => 2018-03-29 10:56:31 [1] => 2018-03-29 10:58:09 [2] => 2018-04-12 11:28:41 [3] => 2018-04-12 13:07:25 [4] => 2018-05-09 13:08:07 )
At the bottom of the same .php page I have this:
<script type="text/javascript">
var sessions = new Array('<?php echo json_encode($savedRequestDates); ?>');
console.log(sessions[0]);
</script>
The console.log(sessions[0]);
returns:
["2018-03-29 10:56:31","2018-03-29 10:58:09","2018-04-12 11:28:41","2018-04-12 13:07:25","2018-05-09 13:08:07"]
Why is the JavaScript array flattening at the 0 index? If I try console.log(sessions);
it returns an array with one variable, not 5, as the php array clearly shows.
Am I missing something here?
This happens because you're wrapping the array from PHP which another array (new Array
). Just remove the new Array
part and it will work fine.
var sessions = <?php echo json_encode($savedRequestDates); ?>;
In this code the function sayHi() doesn't write text with style. Why? How do I apply CSS class to a document.write in a function? .class1 { color: red; font-size: 400%; } <script ...
In this code the function sayHi() doesn't write text with style. Why? How do I apply CSS class to a document.write in a function? .class1 { color: red; font-size: 400%; } <script ...
I am writing tests for a React app using Jest and Enzyme. Enzyme 3.x introduced Adapters to provide compatibility across different versions of React. The installation documentation gives examples how ...
I am writing tests for a React app using Jest and Enzyme. Enzyme 3.x introduced Adapters to provide compatibility across different versions of React. The installation documentation gives examples how ...
In PHP , I've used traits before which is a nice way of separating out reusable code & generally making things more readable. Here is a specific Example: ( trait and class can be in separate ...
In PHP , I've used traits before which is a nice way of separating out reusable code & generally making things more readable. Here is a specific Example: ( trait and class can be in separate ...
I'm stuck with this problem for 3 days now... Someone please help me. Challenge 5 Construct a function intersection that compares input arrays and returns a new array with elements found in all ...
I'm stuck with this problem for 3 days now... Someone please help me. Challenge 5 Construct a function intersection that compares input arrays and returns a new array with elements found in all ...