1

I am trying to send an array back to Javascript from PHP. I'm using Ajax to do this. I know how to send arrays to Javascript, but this time I'm trying to send an array that contains some Javascript constructors. For example I want to be able to send this: [new Date(2017,06,03), 25, 33] Is there a way to send this array to Javascript? If so, how will Javascript be able to use this array in a way where a new Date is created in the first index of the array?

Bytes
  • 691
  • 1
  • 7
  • 22
  • 1
    Well, there is `eval` function which can take a string and execute it as code: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/eval But there are a lot of arguments against using that. If you need to use it (or you think you need to), it can often point out that your design could be improved (not always of course). Why not just send a date in your preferred format and create the actual object in JS after getting the data? – Giedrius Jul 23 '17 at 18:31
  • @Giedrius I need to convert the date within the array because I'm using Google Charts. So essentially, I'm sending back an array of already defined values so Google Charts can take it. – Bytes Jul 23 '17 at 18:39
  • What do you need to convert the date to? Can you provide a specific example? – kmoser Jul 23 '17 at 18:41
  • Yes an example would useful. If you need send an actual `Date` object from PHP to JS I don't think that's possible since it's not a prime type of data that can be understood by both languages independently. Perhaps you should read the API Docs you're using, as they might be accepting certain values that will be automatically converted to the object you need. Or get raw data from your PHP script to your JS script, convert to objects, and then send to the API (which will still be stringified if it's sending via AJAX request). – Giedrius Jul 23 '17 at 18:48
  • 1
    If it's only date related you have some clue here https://stackoverflow.com/questions/4511705/how-to-parse-json-to-receive-a-date-object-in-javascript – quazardous Jul 23 '17 at 18:59

2 Answers2

1

Can not you just simply send value of new Date() instead of constructor? Will be more effective i assume.

  • I need to convert the date within the array because I'm using Google Charts. So essentially, I'm sending back an array of already defined values so Google Charts can take it. – Bytes Jul 23 '17 at 18:38
1

Short answer: No, there is no direct way.

Long answer: When you say that you send arrays to Javascript from PHP, you need to understand that PHP is a server side language, and Javascript is what runs in the browser. So, in between them, u have the network layer and all the things that transfers on that layer should be serializable, usually means strings.

So the data that you pass to the browser via Ajax usually is JSON. It can contain only primitive values, like: string, number, boolean, null.

new Date(2017,06,03) is an javascript object, that is not part of JSON spec. You can serialize the Date with some PHP func like date, send it via Ajax, and then deserialize it on Javascript world.

Something like that:

<?php
    /* your Ajax return */
    echo json_encode(array(date('c', mktime(0, 0, 0, 7, 1, 2000)), 25, 33)); // will return ["2000-07-01T00:00:00-07:00",25,33]
?>

Then on the JavaScript part u can parse it with Date constructor.

// Javascript
const response = ...
const myData = new Data(response[0]); // Will be Date obj.
felixmosh
  • 32,615
  • 9
  • 69
  • 88