title: Create a json file from an array with php, return it and use it with javascript from a javascript file in wordpress
body: <p>Here's my problem: I created a json file with php and I need to access it with javascript (ajax?) in order to print some result on my map.</p>
  
  <p>For me it's very hard, but I'm giving a try step by step.</p>
  
  <p>Here's the first step:</p>
  
  <pre><code>function phptojson($prog = null , $prov = null, $slang = null , $fees = null) {
  
      $posts = getData(null , null, null, null);
      $data = [];
  
      foreach($posts as $post) {
  
          $data[$post-&gt;ID] = [
              "program_type" =&gt; get_post_meta( $post-&gt;ID, 'program_type', true ),
              "province" =&gt; get_post_meta( $post-&gt;ID, 'province', true ),
              "language" =&gt; get_post_meta( $post-&gt;ID, 'language_of_study', true ),
              "tuition" =&gt; get_post_meta( $post-&gt;ID, 'tuition_fees', true ),
              "lenght" =&gt; get_post_meta( $post-&gt;ID, 'length_of_study', true ),
              "title" =&gt; $post-&gt;post_title,
              "location" =&gt; get_post_meta( $post-&gt;ID, 'location', true ),
              "information" =&gt; get_post_meta( $post-&gt;ID, 'details', true ),
          ];
  
      }
      return json_encode($data);
  
  }
  </code></pre>
  
  <p>getData is a function that will call some parameter in order to filter my query, and return data from the database that I need. The second step is the loop, which is working fine, and finally I return my json file.</p>
  
  <p>Now my trouble, which is: what could I do with this file. The older code that I'm improving was using a script tag in order to print the file on the HTML document to access it.</p>
  
  <p>I want to call my funtion phptojson from my javascript file in order to access it. I've aready created the google maps, which is working fine, so I only need to print the result from my javascript function. My file hierarcy looks like that:</p>
  
  <p>wp-content
    -themes
      -mytheme
       myfunction.php (where I put phptojson() )
         -js
            myscript.js</p>
  
  <p>Essentially I want to call my function from myscript.js with ajax and I'm trying this approach that I found here, but it's not working:</p>
  
  <pre><code>function getJsonElement() {
      $.get('/includes/custom-function.php', function(phptojson) {      
      console.log(data);
  } );
  }
  </code></pre>
  
  <p>Which kind of makes sense.. What could it be my next step?</p>
  
  <p>PS. My json file looks like:</p>
  
  <pre><code>string(162036) "{"1439":
  {"program_type":"long","province":"NY","language":"EN","tuition":"10","lenght":"full","title":"My School","location":"10 awesome street, NY, US"}}
  </code></pre>