title: How to get MySQL data into correct array structure
body: <p>I just finished getting a script to work by using a hard coded array like so:</p>
  
  <pre><code>dataArray[0] =[50,10,0.3,0.25,50,"FFF",3];
  dataArray[1] =[50,10,0.3,0.2,50,"FFF",3];
  ....
  dataArray[5] =[50,20,0.5,0.7,120,"FF0",4];
  </code></pre>
  
  <p>I put the contents of dataArray into a db table so I could eliminate the hard coded array.<br>
  I'm returning the data in a string from MySql with PHP, and a data dump shows that the number of values and the values themselves are correct.  </p>
  
  <p>However, I'm apparently not coding the data capture correctly because I'm not ending up with an array like my hard coded array (I need to maintain the structure of my hard coded array). </p>
  
  <p>Here's the code for the db read:</p>
  
  <pre><code>function getSomeData(){ 
      $.ajax({
          type: "GET",
          url: "GetSomeData.php",
          dataType: "text",
          success:function(result){
              setSomeData(result);
          }
      });
  }
  
  var someDataArray = new Array();
  function setSomeData(resultData){ 
      var resData = resultData.split('^');//record split  
      for(var i = 0; i &lt; resData.length; i++){
          someDataArray[i] = resData[i].split('#');//field split
          someDataArray[i].pop();//removes array element occupied by '^'
  
          if(i == resData.length - 1){
              setDataArray();
          }
      }
  }
  </code></pre>
  
  <p>This for loop in setDataArray() doesn't create the correct array structure because someDataArray[x] is just a string:</p>
  
  <pre><code>var dataArray = new Array();
  function setDataArray(){ 
      for(var i = 0; i &lt; dataArray.length; i++){
          dataArray[i] = someDataArray[i];
      }
  }
  </code></pre>
  
  <p>So I tried putting someDataArray[x] in an array like so:</p>
  
  <pre><code>dataArray[i] = [someDataArray[i]];
  </code></pre>
  
  <p>But that didn't work either.</p>
  
  <p>I've spent the 2 days trying to puzzle this out, reading blogs, and experimenting with everything I could think of, but no luck. I think it's a simple solution but I just can't get it.</p>
  
  <p>Help?</p>
  
  <hr>
  
  <hr>
  
  <p><strong>EDIT:</strong></p>
  
  <p>After learning a bit about JSON and json_encode I now have my script working.  I wanted to post the way I did it to acknowledge that I received some valuable advice from Pat Burke.  The code below may not be what he had in mind but I massaged it until it worked. But I don't really understand why it works, so I'll have to do some more reading on json_encode I think.  </p>
  
  <p>Note that using dataType:"json" in the ajax call threw an error.</p>
  
  <p>//GetSomeData.php</p>
  
  <pre><code>$return_arr = array();
  
  $query1 = "SELECT * FROM mytable ORDER BY idx ASC";
  $result = mysql_query($query1) or die('Query failed: ' . mysql_error());
  
  while($row = mysql_fetch_array($result)){  
      $resultArray = array();
      $resultArray[] = (int)$row['dc'];
      $resultArray[] = (int)$row['smlspc'];
      $resultArray[] = (float)$row['sclx'];
      $resultArray[] = (float)$row['scly'];
      $resultArray[] = (int)$row['lgspc'];
      $resultArray[] = (int)$row['colr'];
      $resultArray[] = (int)$row['diam'];
      if(count($resultArray) ==  7){
          array_push($return_arr, $resultArray);
      }
  }
  echo json_encode($return_arr);
  
  mysql_free_result($result);
  </code></pre>
  
  <p>//new js</p>
  
  <pre><code>function getSomeData(){
      resultData = new Array();
      $.ajax({
          type: "GET",
          url: "GetSomeData.php",
          dataType: "text",
          //dataType: "json", //using this threw an error (see below)
          success:function(result){
              resultData = $.parseJSON(result);
              $("#p1").append("resultData.length =" + resultData.length + "&lt;br /&gt;");
              //resultData.length =114 (it's a string not an array)
              $("#p1").append("resultData =" + resultData + "&lt;br /&gt;");
              //resultData =
              //[[50,10,0.375,0.25,50,0,0],
              //[50,10,0.3,0.2,50,0,1],
              //[50,10,0.6,0.4,0,0,2],
              //[50,0,0.4,0.4,0,0,3],
              //[50,0,0.4,0.4,0,0,3]]
  
              for(var i = 0; i &lt; resultData.length; i++){
                  $("#p1").append("resultData[" + i + "] =" + resultData[i] + "&lt;br /&gt;");
                  //data displayed with dataType: "text"
                  //resultData[0] =50,10,0.375,0.25,50,0,0
                  //resultData[1] =50,10,0.3,0.2,50,0,1
                  //resultData[2] =50,10,0.6,0.4,0,0,2
                  //resultData[3] =50,0,0.4,0.4,0,0,3
                  //resultData[4] =50,0,0.4,0.4,0,0,3
  
                  //data displayed with dataType: "json"
                      //resultData is null
              }
          },
          //this has no effect with either dataType: "text" or dataType: "json"
          contentType: 'application/json'
      });
  }
  </code></pre>