JavaScript Why does PHP json_encode flatten array?

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?

Answer:1

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); ?>;
Answer:2

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 ...

  1. word cannot apply style
  2. cannot apply paragraph style

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 ...

  1. configure enzyme adapter
  2. configure enzyme adapter jest
  3. enzyme.configure( adapter new enzyme adapter() )
  4. jest configure enzyme adapter
  5. enzyme.configure( adapter new adapter() )
  6. enzyme-adapter-react-16 configure

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 ...

  1. javascript compare input value
  2. javascript compare input date
  3. javascript compare input string
  4. javascript compare input text
  5. javascript compare two input values
  6. javascript compare two input fields
  7. javascript compare user input
  8. javascript compare 2 input fields
  9. javascript compare user input string
  10. javascript compare two dates input
  11. compare input javascript
  12. javascript compare 2 input
  13. javascript compare two input