我的 PHP foreach 循环输出 print_r 正确但不是 file_put_contents

My PHP foreach loop outputs print_r correctly but not file_put_contents

我正在使用 simple_html_dom 解析页面和 return 一些内容。

当我尝试在 foreach 循环中输出带有 print_r 的内容时,它 return 包含所有元素。但是,如果我尝试将内容输出到文本文件,它只会输出最后一个元素。我做错了什么?

这是我的示例代码:

include 'simple_html_dom.php';

$partlist_file = $_SERVER['DOCUMENT_ROOT'].'/partlist.txt';

$partlist = file('knn-partnumberlist.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

$stock = '';

    $output_first = '';
    foreach($partlist as $parts => $part) {

        $html = file_get_html('search/product.aspx?prod=' . $part);
        $ret = $html->find('span#cph_lb_stock_buy'); 

        foreach($ret as $element) {
            $stock = $element->plaintext;
            $stock = preg_replace(array('/\n/','/\r/'),'',$stock);
            $stock = trim($stock);
            if($stock == 'Not in stock.') {
                $stock = '0';
            } elseif($stock == 'In Stock & Ready to Ship!') {
                $stock = '6';
            }



        $output = 'K33' . $part . ',' . $stock . "\n";
        print_r ($output); // returns all elements
        file_put_contents($partlist_file, $output); // only returns last element

        }
    }

print_r 输出示例:

K3300283,6
K3301518,6
K3301988,6
K3303351,6
K3303365,6

file_put_contents 输出示例:

K3303365,6

您需要为每次迭代连接 $output,否则它会被简单地覆盖

 $output .= 'K33' . $part . ',' . $stock . "\n";
        print_r ($output);
        file_put_contents($partlist_file, $output);

.是串联运算符

print_r 函数有一个布尔选项,允许它作为字符串变量返回,而不是将其打印到屏幕上。它的用法是这样的:

$myVar = print_r($otherVar, true);

这将允许您收集值,而不是将其直接打印到输出缓冲区(例如屏幕)。这将被证明是有用的,因为您现在可以根据需要或期望操纵该值。

此外,file_put_contents 还有一个选项允许您附加到文件,而不是覆盖它的当前内容。同样,用法如下所示:

$dummy = file_put_contents('path/to/file', $varToSave, FILE_APPEND);

最后一个选项是一个整数位标志(我不知道它的实际值)告诉 PHP 将新数据添加到文件末尾而不是用新的替换当前内容数据。这两个选项的使用在您的原始代码中看起来像这样:

include 'simple_html_dom.php';

$partlist_file = $_SERVER['DOCUMENT_ROOT'].'/partlist.txt';

$partlist = 文件('knn-partnumberlist.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

$stock = '';

$output_first = '';
foreach($partlist as $parts => $part) {

    $html = file_get_html('search/product.aspx?prod=' . $part);
    $ret = $html->find('span#cph_lb_stock_buy'); 

    foreach($ret as $element) {
        $stock = $element->plaintext;
        $stock = preg_replace(array('/\n/','/\r/'),'',$stock);
        $stock = trim($stock);
        if($stock == 'Not in stock.') {
            $stock = '0';
        } elseif($stock == 'In Stock & Ready to Ship!') {
            $stock = '6';
        }



    $output = 'K33' . $part . ',' . $stock . "\n";
    $display = print_r ($output, true);
    // you can manipulate $display here, like so:
    $display = str_replace("\n", "\r\n", $display);
    echo $display; // returns all elements (one at a time)
    file_put_contents($partlist_file, $display, FILE_APPEND); // now stores all values of $output in the file

    }
}

当然,看起来你并不真的需要在你的脚本中操作 $output 的值,但是为了说明 print_r 中选项的用处,我在一行将 Linux 换行符 (\n) 替换为 Windows 换行符 (\r\n) 的代码,以便像记事本这样的程序可以正确显示文本 (PHP 的 print_r 函数只在输出中放置 \n 换行符,即使在 Windows 机器上也是如此,这就是我发现这个 post 的原因。我试图找到一种方法来纠正这种行为)。这一次可能对你没有用,但它可能会在以后证明对遇到这个问题的其他人有用。我希望这有帮助。 :)