I am new to XML but am trying to use dhtmlxGrid for one of my sites. I am reading a text file and putting the data into an array. It seems that most of the examples I see use associative arrays to convert to XML.

I have:

Array ( 
[0] => Array ( [0] => 1 [1] => 243.4002 [2] => 0.9099 ) 
[1] => Array ( [0] => 2 [1] => 222.1930 [2] => 0.9092 ) 
)

But probably need:

Array ( 
[0] => Array ( [rownum] => 1 [reading1] => 243.4002 [reading2] => 0.9099 ) 
[1] => Array ( [rownum] => 2 [reading1] => 222.1930 [reading2] => 0.9092 ) 
)

This is how I am creating my array:

  
$filename = $_FILES["file"]["tmp_name"]; $file_handle = fopen($_FILES["file"]["tmp_name"], "r"); while (!feof($file_handle)) { $line = fgets($file_handle); $counter = $counter+1; if ($counter >= 9) { $splitLine[] = explode("\t", $line); }

I am struggling figuring out how to create an associative array here. TIA.

    Do you mean like this?

    $temp =  explode("\t", $line);
    
    $splitLine[] = array('rownum'=>$temp[0], 'reading1'=>$temp[1], 'reading2'=>$temp[2]);
      function add_indices($row)
      {
          $names = array('rownum', 'reading1', 'reading2');
          $values = explode("\t", $row);
          return array_combine($names, $values);
      }
      
      $rows = array_slice(file($_FILES["file"]["tmp_name"]), 8); // skip first eight lines
      $rows = array_map('add_indices', $rows);
      

        Both of these solutions work great. 🙂

          Write a Reply...