title: how to use json_encode without PHP 5.2
body: <p>I've written a CMS which uses the PHP function json_encode to send some data back via an Ajax Request.</p>
  
  <p>Unfortunately, I'm trying to load it onto a server which is running PHP version 5.1, the json_encode PHP function is not available on versions of PHP before 5.2.0.</p>
  
  <p>Does anyone know of a way to encode a PH array as JSON without using the inbuilt json_encode function?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I've used Pekka's function, but now I find JQuery won't parse the result as expected. Even though Firebug shows JSON being passed back through. My firebug window looks like this:<a href="https://i.stack.imgur.com/57gW8.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/57gW8.jpg" alt="alt text"></a>
  </p>
  
  <p>and my jquery looks like this: </p>
  
  <pre><code>     $.ajax({
              type: "GET",
              url: "includes/function/add_users.php",
              data: str,
              dataType: 'json',
              cache: false,
              beforeSend: function(html){
                      $('#editbox').html('&lt;img class="preloader" src="images/ajax-loader.gif"/&gt;');
              },
  
              success: function(html){
                  fields = html;
                  $('#div1').html(fields['username']);
                  $('#div2').html(fields['fname']);
  </code></pre>
  
  <p>But the divs : #div1 and #div2 will not load the correct data.</p>
  
  <p><strong>FOR WHY?</strong></p>