php - 将 php 关联数组写入文本文件

标签 php mysql fwrite

我想将一个 mysql_fetch_assoc(它返回一个关联数组)值写入一个文本文件, 虽然我想以如下方式获得输出

ID => 17
CODE => 4
Value => 59559

它只是像下面这样打印

17
4
59595
25
0

所以这是我目前用来获取结果的代码,有人可以帮助我吗

$query = "CALL pro_details($ID, '$start', '$end', $limit, $pos);";
        $result = mysql_query($query, $con);
        $myFile = "debug.txt";
        $fh = fopen($myFile, 'w') or die("can't open file");
        while($stringData_2 = mysql_fetch_assoc($result)){
                foreach ($stringData_2 as $string) {
                    fwrite($fh, $string);
                    $stringbreak = "\n";
                    fwrite($fh, $stringbreak);
                }
                $stringbreak = "----------------\n";
                fwrite($fh, $stringbreak);
        }
        fclose($fh);

最佳答案

将您的 fwrite() 调用更改为:

foreach ($stringData_2 as $key=>$string) {
    fwrite($fh, $key ." => ".$string);
    $stringbreak = "\n";
    fwrite($fh, $stringbreak);
}

关于php - 将 php 关联数组写入文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27286666/

相关文章:

python - 使用Python将空格 ""插入表文件中以分隔列

javascript - 使用 html 按钮运行 python 脚本

php - 点击立即付款按钮后 Paypal 会产生任何事件吗?

php - 如何禁用 phpmailer 消息

PHP mysql如何连接两个表以链接一个表中的名称并从另一个表中发布

c - 写入大文件时带宽读数增加

php - 如何确定是否在 PHP 中启用了 PDO?

php - 如果我更改查询中的值,回显结果将保持不变

php - 一次从多个 url 获取 XML 数据失败

使用 fwrite 复制文本文件,结果是垃圾? C