如何在 php 中用基于 for 循环的数据附加文件


How to append a file with for loop based data in php

我想使用 php 将 txt 文件与 for 循环数据附加。这是我的代码

$post_array = array('hdnItemTypeID', 'hdnItemType', 'hdnItemCode',  'hdnCost', 'txtDescription');
        foreach ($post_array as $pos) {
            foreach ($_POST[$pos] as $id => $row) {
                $_POST[$pos][$id] = $row;
            }
        }
$ids = $_POST['hdnItemTypeID'];
        $ItemTypeID = $_POST['hdnItemTypeID'];
        $ItemType = $_POST['hdnItemType'];
        $ItemCode = $_POST['hdnItemCode'];
        //$Discount = $_POST['txtDiscount'];
        $Discount = '0';
        $UnitCost = $_POST['hdnCost'];
        //$Amount = $UnitPrice + $Discount;
        $Description = $_POST['txtDescription'];
        $size = count($ids);
 for ($i = 0; $i < $size; $i++) {
            // Check for part id
            if (empty($ids[$i])) {
                continue;
            }
            $items = array(
                ':ItemTypeID' => $ItemTypeID[$i],
                ':ItemType' => $ItemType[$i],
                ':ItemCode' => $ItemCode[$i],
                ':Description' => $Description[$i],
                ':UnitCost' => $UnitCost[$i],
                ':Amount' => $UnitCost[$i] +$Discount,
                ':Discount' => $Discount
            );
            file_put_contents('Check.txt', print_r($items,true));
        }

但是只有单个数组正在进入文件。如何获取其中的所有数组值?

默认情况下

file_put_contents会覆盖所有内容。用:

file_put_contents('Check.txt', $items, FILE_APPEND);

参考

我认为你不能使用

file_put_contents('Check.txt', print_r($items,true), FILE_APPEND);

见 http://php.net/manual/fr/function.file-put-contents.php