数组问题,键加倍并留空键

时间:2011-08-28 22:53:48

标签: php mysql arrays

我的代码出现问题。当我执行返回数组的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";
                                }
                            }
                            */
                        }

当我执行$ record的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
)

1 个答案:

答案 0 :(得分:0)

很久以前发现了答案!