2011-08-28 44 views
0

我遇到了我的代碼問題。當我對返回的數組執行print_r操作時,它似乎在做的事情是,它具有帶有值的關聯關鍵字,然後是不返回值的數字鍵,最後,當我將這些數組值輸出到.csv文件時最終將輸出的信息加倍,並且看起來像陣列的尺寸是最後的兩倍。我怎樣才能修復我的陣列,使信息不會翻倍。我的代碼是波紋管:陣列問題,鍵是加倍並留下空鍵

   if ($_GET['do']=="export"){ 
     if (isset($_POST[format])){ 
      foreach ($_POST as $k=>$v){ 
       if (intval($k)>0){ 
        $selstr[]="field$k"; 
        $idstr[]=$k; 
       } 
      } 
       if (is_array($selstr)){ 
//     $data=$db->qarray("select ".implode(",",$selstr)." from business_db;"); 
        $fields=$db->qarray("select * from business_fields where id in (".implode(",",$idstr).");"); 

        $toget = ""; 

        foreach($fields as $field) 
        { 
         $toget .= "field{$field[id]},"; 
        } 


        $toget = substr($toget,0,strlen($toget)-1);     

        $dates = $db->qarray("SELECT * FROM business_years"); 


        if ($_POST[format]=="csv"){ 
         header("Pragma: public"); 
         header("Expires: 0"); 
         header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
         header("Content-Type: application/force-download"); 
         header("Content-Type: application/octet-stream"); 
         header("Content-Type: application/download");; 
         header("Content-Disposition: attachment;filename=export.csv "); 
         header("Content-Transfer-Encoding: binary "); 
         foreach($fields as $f){ 
          $headers[]=$f[name]; 
         } 
         echo implode(",",$headers); 
         echo "\n"; 
         if ($_POST[filterdate]=="yes"){ 
          //$sd=mktime(0,0,-1,intval($_POST[startmonth]),1,intval($_POST[startyear])); 
          //$ed=mktime(24,0,1,intval($_POST[endmonth]),date("t",mktime(1,1,1,intval($_POST[endmonth]),1,intval($_POST[endyear]))),intval($_POST[endyear])); 
          $ids = $db->qarray("SELECT DISTINCT bus_id FROM business_years WHERE date_registered BETWEEN '{$startyear}-{$startmonth}-00' and '{$endyear}-{$endmonth}-31'"); 

          $idsa = ""; 
          foreach($ids as $id) 
          { 
           $idsa .= $id[bus_id] . ","; 
          } 

          $idsa = substr($idsa,0,strlen($idsa)-1); 


          $records = $db->qarray("SELECT {$toget} FROM business_db WHERE id IN({$idsa})"); 

          $output = ""; 
          foreach($records as $record) 
          { 
           $output .= implode(",",$record) . "\n"; 
          } 

          $rc = print_r($record,true); 

          $file = "debugdirectory.txt"; 
          $fh = fopen($file,'w'); 

          $string = $rc; 



          fwrite($fh,$string); 
          fclose($fh); 

          /* 
          foreach ($data as $d){ 
           $cd=strtotime($d[field2]); 
           if ($cd > $sd and $cd < $ed){ 
           $values=array(); 
           foreach($fields as $f){ 
            $fname="field$f[id]"; 
            $values[]=$d[$fname]; 
           } 
           echo implode(",",$values); 
           echo "\n"; 
           } 
          } 
          */ 
         } 

當我做的一個$記錄的print_r返回:

 Array 
(
    [0] => 
    [field3] => 
    [1] => 
    [field4] => 
    [2] => 
    [field5] => 
    [3] => 
    [field6] => 
    [4] => 
    [field7] => 
    [5] => 
    [field8] => 
    [6] => 
    [field9] => 
    [7] => 
    [field10] => 
    [8] => 
    [field11] => 
    [9] => 
    [field13] => 
    [10] => 
    [field14] => 
    [11] => 
    [field15] => 
    [12] => 
    [field16] => 
    [13] => 
    [field17] => 
    [14] => 
    [field18] => 
    [15] => Regular 
    [field19] => Regular 
    [16] => 
    [field20] => 
    [17] => 
    [field21] => 
    [18] => Yes 
    [field31] => Yes 
    [19] => 
    [field23] => 
    [20] => 
    [field24] => 
    [21] => Yes 
    [field30] => Yes 
    [22] => 
    [field28] => 
    [23] => 01/01/2008,01/01/2009,01/01/2010 
    [field32] => 01/01/2008,01/01/2009,01/01/2010 
) 
+0

「問題」與數據庫查詢有關。如果您使用標準的'mysql_query()'函數,那麼將'MYSQL_ASSOC'放入您的'mysql_fetch_array()'函數中會很有必要。作爲第二個參數。 – ty812

回答

0

答發現前一段時間!