title: Using json_encode() in PHP and JSON.parse() in JavaScript to read/write from/to a file
body: <p>I'm using <code>json_encode()</code> in <code>PHP</code> to write an array into a file and after that I use <code>JSON.parse()</code> in <code>JavaScript</code> on the client side to read the json encoded file and pass it as an array to a sorting algorithm:</p>
  
  <blockquote>
    <ul>
    <li><p>My json_encode() output file:
    ["1","96","32","33","4","48","74","19","23","43","8","8","46","36","92","81","81","64","26","96","82","85","80","24","61","4","46","32","68","11","63","14","98","20","66","17","28","58","32","16","33","47","80","94","5","68","35","28","24","85","38","12","79","57","6","47","18","15","34","18","91","63","67","73","86","16","71","29","14","79","18","10","97","29","1","97","72","92","42","19","25","76","38","25","21","37"]</p></li>
    <li><p>After using JSON.parse() I get my array back in a variabel <code>unsortedArray</code> with:</p></li>
    <li><p>Index: 0</p></li>
    <li><p>Value: 1,96,32,33,4,48,74,19,23,43,8,8,46,36,92,81,81,64,26,96,82,85,80,24,61,4,46,32,68,11,63,14,98,20,66,17,28,58,32,16,33,47,80,94,5,68,35,28,24,85,38,12,79,57,6,47,18,15,34,18,91,63,67,73,86,16,71,29,14,79,18,10,97,29,1,97,72,92,42,19,25,76,38,25,21,37</p></li>
    </ul>
  </blockquote>
  
  <p>But when I put it through a sorting algorithm like for example Bublesort (I know it's not the best one) I get a strange output:</p>
  
  <pre><code>[0] =&gt; Array
          (
              [0] =&gt; 1
              [1] =&gt; 1
              [2] =&gt; 10
              [3] =&gt; 11
              [4] =&gt; 12
              [5] =&gt; 14
              [6] =&gt; 14
              [7] =&gt; 15
              [8] =&gt; 16
              [9] =&gt; 16
              [10] =&gt; 17
              [11] =&gt; 18
              [12] =&gt; 18
              [13] =&gt; 18
              [14] =&gt; 19
              [15] =&gt; 19
              [16] =&gt; 20
              [17] =&gt; 21
              [18] =&gt; 23
              [19] =&gt; 24
              [20] =&gt; 24
              [21] =&gt; 25
              [22] =&gt; 25
              [23] =&gt; 26
              [24] =&gt; 28
              [25] =&gt; 28
              [26] =&gt; 29
              [27] =&gt; 29
              [28] =&gt; 32
              [29] =&gt; 32
              [30] =&gt; 32
              [31] =&gt; 33
              [32] =&gt; 33
              [33] =&gt; 34
              [34] =&gt; 35
              [35] =&gt; 36
              [36] =&gt; 37
              [37] =&gt; 38
              [38] =&gt; 38
              [39] =&gt; 4
              [40] =&gt; 4
              [41] =&gt; 42
              [42] =&gt; 43
              [43] =&gt; 46
              [44] =&gt; 46
              [45] =&gt; 47
              [46] =&gt; 47
              [47] =&gt; 48
              [48] =&gt; 5
              [49] =&gt; 57
              [50] =&gt; 58
              [51] =&gt; 6
              [52] =&gt; 61
              [53] =&gt; 63
              [54] =&gt; 63
              [55] =&gt; 64
              [56] =&gt; 66
              [57] =&gt; 67
              [58] =&gt; 68
              [59] =&gt; 68
              [60] =&gt; 71
              [61] =&gt; 72
              [62] =&gt; 73
              [63] =&gt; 74
              [64] =&gt; 76
              [65] =&gt; 79
              [66] =&gt; 79
              [67] =&gt; 8
              [68] =&gt; 8
              [69] =&gt; 80
              [70] =&gt; 80
              [71] =&gt; 81
              [72] =&gt; 81
              [73] =&gt; 82
              [74] =&gt; 85
              [75] =&gt; 85
              [76] =&gt; 86
              [77] =&gt; 91
              [78] =&gt; 92
              [79] =&gt; 92
              [80] =&gt; 94
              [81] =&gt; 96
              [82] =&gt; 96
              [83] =&gt; 97
              [84] =&gt; 97
              [85] =&gt; 98
          )
  </code></pre>
  
  <p>I call the function with the sorting algorithm and the input like this <code>bubleSort.apply(this, unsortedArray);</code>.</p>
  
  <p>But as shown above it's not completely sorted, everything except the numbers 1 (just accidently in the right place), 4, 5, 6 and 8 is sorted, I don't understand why this is happening.</p>
  
  <p><strong>Update:</strong></p>
  
  <p>The <code>$jobRetParam</code> has all the results that comes from another page, it's an output of a JavaScript code being sent using <code>POST</code> to the site with this code:</p>
  
  <pre><code>$jobRetParam = $_REQUEST['results'];
  </code></pre>
  
  <p>The $allOutputVarPath variable is created here:</p>
  
  <pre><code>$allOutputVarPath = array();
      $i = 0;
      foreach ($allOutputVar as $key =&gt; $values) {
          foreach ($values as $key =&gt; $value) {
              if ($key == 4) {
                  $allOutputVarPath[] = array($value =&gt; $jobRetParam[$i]);
                  $i++;
              }
          }
      }
  </code></pre>
  
  <p>The <code>$allOutputVar</code> comes from a Database the <code>$key == 4</code> is where the <code>$value</code>holds the path that I'm using after that in the <code>$path</code> variable in the loop below. The <code>$jobRetParam[$i]</code> is explained just under the "<strong>Update</strong>" word, each <code>$value</code> gets his set of results.</p>
  
  <p>I have a lot of code but at the end it comes to this when encoding to <code>Json</code>:</p>
  
  <pre><code>foreach ($allOutputVarPath as $key =&gt; $values) {
          foreach ($values as $path =&gt; $fileContent) {
              $writeSuccess = wrtieToHDD($path, "w", $fileContent);
          }
  }
  </code></pre>
  
  <blockquote>
    <p>Hier is an example of a <code>$path =&gt; $fileContent</code> that I use as an input in the <code>wrtieToHDD()</code> function where I encode the <code>$fileContent</code> as <code>Json</code>.</p>
    
    <ul>
    <li><p><code>$path</code>: [.../sorted2.txt] </p></li>
    <li><p><code>$fileContent</code>: Array ( [0] => 1 [1] => 1 [2] => 10 [3] => 11 [4] => 12 [5] => 14 [6] => 14 [7] => 15 [8] => 16 [9] => 16 [10] => 17 [11] => 18 [12] => 18 [13] => 18 [14] => 19 [15] => 19 [16] => 20 [17] => 21 [18] => 23 [19] => 24 [20] => 24 [21] => 25 [22] => 25 [23] => 26 [24] => 28 [25] => 28 [26] => 29 [27] => 29 [28] => 32 [29] => 32 [30] => 32 [31] => 33 [32] => 33 [33] => 34 [34] => 35 [35] => 36 [36] => 37 [37] => 38 [38] => 38 [39] => 4 [40] => 4 [41] => 42 [42] => 43 [43] => 46 [44] => 46 [45] => 47 [46] => 47 [47] => 48 [48] => 5 [49] => 57 [50] => 58 [51] => 6 [52] => 61 [53] => 63 [54] => 63 [55] => 64 [56] => 66 [57] => 67 [58] => 68 [59] => 68 [60] => 71 [61] => 72 [62] => 73 [63] => 74 [64] => 76 [65] => 79 [66] => 79 [67] => 8 [68] => 8 [69] => 80 [70] => 80 [71] => 81 [72] => 81 [73] => 82 [74] => 85 [75] => 85 [76] => 86 [77] => 91 [78] => 92 [79] => 92 [80] => 94 [81] => 96 [82] => 96 [83] => 97 [84] => 97 [85] => 98 ) </p></li>
    </ul>
  </blockquote>
  
  <p>And the <code>wrtieToHDD()</code> function:</p>
  
  <pre><code>function wrtieToHDD ($destination, $fopenMode, $content) {
      $handle = fopen($destination, $fopenMode);
  
      // check if $content is an array
      if (gettype($content) == "array" or "object") {
  
          // encode as json
          $content = json_encode($content);
      }
  
      // write into file
      $writeContent = FALSE;
      if (flock($handle, LOCK_EX)) { // exclusiv lock
          fwrite($handle, $content);
          fflush($handle); // clear the output buffer bevor the lock free
          flock($handle, LOCK_UN); // free lock
          $writeContent = TRUE;
      }
  
      fclose($handle);
      return $writeContent;
  }
  </code></pre>