title: php multidimensional array to js
body: <p>I know this question is a duplicate but I really would like some help with this. I know the only way to send a php array to js is by encoding with json. Well I tried that but nothing works. Here's my code:</p>
  
  <pre><code>&lt;?php
  
  Header("content-type: application/x-javascript");
  error_reporting(E_ERROR | E_WARNING | E_PARSE);
  
      $months = Array();
  
      $months = ['January'=&gt;array(), 'February'=&gt;array(), 'March'=&gt;array(), 'April'=&gt;array(), 'May'=&gt;array(), 'June'=&gt;array(), 'July'=&gt;array(), 'August'=&gt;array(), 'September'=&gt;array(), 
                          'October'=&gt;array(), 'November'=&gt;array(), 'December'=&gt;array() ]; 
  
  
  
      // Connect to MySQL
           if ( !( $database = mysql_connect( "localhost",
              "root", "" ) ) )                      
              die( "Could not connect to database &lt;/body&gt;&lt;/html&gt;" );
  
      // open Events database
           if ( !mysql_select_db( "Events", $database ) )
              die( "Could not open Events database &lt;/body&gt;&lt;/html&gt;" );
  
              foreach($months as $month =&gt; $arr) {
  
              $result = mysql_query("SELECT * FROM posted_events WHERE Month_ = '$month' ") 
                      or die ('Error updating database because: '.mysql_error());
  
              while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
                  $months[$month][] =  $row['DayNum']; 
  
      }
  }
  
              echo "var months = jQuery.parseJSON('$months');";
  &lt;?php
  </code></pre>
  
  <p>So the JS array variable 'months' should contain all the elements that was in the PHP's  array variable right? Well it doesn't work. I don't know what's wrong. Help please? I need to use this array in my external JS file to do some more work with my program, so it's really important. Thanks guys.</p>