Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用php在文件中添加列_Php - Fatal编程技术网

使用php在文件中添加列

使用php在文件中添加列,php,Php,我已经创建了一个文件file1.out,并且正在向其中输入数据。 $parameters是包含参数名称的数组 foreach($parameters as $name) { fwrite(file1.out,$name); //here I am fetching the value of the parameter $name from database while($row=mysql_fetch_

我已经创建了一个文件file1.out,并且正在向其中输入数据。 $parameters是包含参数名称的数组

   foreach($parameters as $name)
     {
           fwrite(file1.out,$name);
           //here I am fetching the value of the parameter $name from      database
            while($row=mysql_fetch_array($result))
             {  
               //getting some values and writing into the file;
             }

     }



        /*  this is giving output as 
             parameter1
              1
             parameter2
              2
             parameter3
              3
           but I want in this way

           parameter1   parameter2   parameter3
              1          2             3 */

在获取特定参数的数据后,需要在文件中写入哪个特殊字符才能获得新列?请帮助。

在每个循环中转到下一行。所以,将它们存储到一个变量中,并在foreach之后将它们写入文件

使用类似以下内容:

<?php

$myfile = fopen("sample.txt", "w");

$names = '';
$values = '';

foreach($parameters as $name) {
           //fwrite(file1.out,$name);
           $names .= "$name \t";
           //here I am fetching the value of the parameter $name from      database
            while($row=mysql_fetch_array($result)) {
                $values .= $row[0] . "\t";
                //getting some values and writing into the file;
             }

}

   //Make sure to remove any \n.
   $values= str_replace("\n", '', $values);
   $names = str_replace("\n", '', $names );

   fwrite($myfile, $names . "\n" . $values);
   fclose($myfile);

在循环中使用2个变量来构建字符串,并使用\t between text选项卡对其进行索引:

$param = ''; $vals = '';
   foreach($parameters as $name)
     {
       $param .= '\t\tparameter1\t\t';
       $vals .= '\t\t\t' . $name . '\t\t\t';
    ......
     }
然后在for循环之后将$param和$vals写入文件。在找到合适的演示文稿之前,您可以使用任意数量的\t