2

Yes, it's been asked a lot. No, I still can't get it to work after days of searching online. (also: yes, I'm still pretty new to php and much of a copy/paste coder). I have an array in PHP called EP. I shuffle EP. I then need the shuffled values of EP passed onto a javascript array.

I checked out all the answers here Convert php array to Javascript but no succes: Spudley's answer uses php and javascript in one file, whereas I'm using seperate files (index.php, data.php, main.js). If I use Eric's answer netbeans tells me 'expected operant but found..'

the array is located in data.php:

$EP = array(30,40,50,60,70,80,90,100,110,120,130,140,150,160,170,180,190,200,210,220);
shuffle($EP);

I need this shuffled array (in the shuffled order) in my main.js, so I can display it in a graph with jqplot. I used this, but then the whole js file basically stops working

var arrayFromPHP = "<?php echo json_encode($EP); ?>";
alert( $.toJSON(arrayFromPHP) );

and I tried the following, but the alert says 'undefined'

$.getJSON("data.php",function($EP){
        alert( $.toJSON($EP) );           
  });

Could someone point out my mistakes?

Community
  • 1
  • 1
ShariDB
  • 159
  • 1
  • 11

2 Answers2

2

That will convert your array

<?php
$EP = array(30,40,50,60,70,80,90,100,110,120,130,140,150,160,170,180,190,200,210,220);
echo '<script>
var ep = [ '. implode(' , ', $EP) .' ];
</script>
';
?>

If you want to use it inside a .js file (be very carefull with the cache), you have to edit your .htaccess file

<FilesMatch "\.js$">
  SetHandler application/x-httpd-php
  Header set Content-type "application/javascript"
</FilesMatch>

To prevent cache js files, edit .htaccess file

<FilesMatch "\.js$>
FileETag None
Header unset ETag
Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
</FilesMatch>
Carlos M. Meyer
  • 436
  • 3
  • 9
  • Thank you for your response. If you say " be very careful with the cache", I'm a bit hesitant to try it given my limited knowledge.. – ShariDB Mar 19 '15 at 11:57
  • You can add a parameter every time you call the js file making a new link every time you call it, like http://..../file.js?timestamp= – Carlos M. Meyer Mar 19 '15 at 12:40
0

okay what you are doing now is the following:

Echo out the JSON object into a javascript variable. Only problem is you surrounded it with quotes. So not its a string.

If you'd look at the source code you'd see

<script type="text/javascript">
var arrayFromPHP = "[110,80,190,60,160,120,150,40,220,170,30,130,200,180,90,70,50,210,100,140]";
alert( $.toJSON(arrayFromPHP) );
</script>

what you need to do is

var arrayFromPHP = <?php echo json_encode($EP); ?>;

Without the quotes you get a fully qualified array instantiated with which you can work.

Now, if you wanted it as JSON format, keep it as it is, no need to convert it to JSON using jquery because it's already a JSON string. You can pass it directly.

All depends on what you need. A JSON String or a fully qualified array.

You can also turn the string into an array by doing JSON.parse(arrayFromPHP) on the string if you choose to keep the quotes to get a fully qualified array.

So there are multiple roads to rome, its up to you to choose which option you need.

Tschallacka
  • 27,901
  • 14
  • 88
  • 133
  • Thank you Michael. If I write var arrayFromPHP = ; Netbeans says this is incorrect code (expected an operand but found <) – ShariDB Mar 19 '15 at 12:01
  • Thats because netbeans expects javascript code and probaly can't handle the nested code. If it's in a .php file the php parser will do its thing. Just run it and let neatbeans shut up. – Tschallacka Mar 19 '15 at 12:02