【发布时间】:2017-09-08 02:19:10
【问题描述】:
我已经为这个问题苦苦挣扎了一个星期了,我别无选择,只能在 stackoverflow 中问一个问题。
我目前正在使用 PHP Spout 读取 xlsx 文件的行,数组看起来像这样
Array
(
[Name] => abcd1234
[Address] => abcd1234
[Number] => abcd1234
)
Array
(
[Name] => abcd1234
[Address] => abcd1234
[Number] => abcd1234
)
Array
(
[Name] => abcd1234
[Address] => abcd1234
[Number] => abcd1234
)
foreach 循环如下所示
// Number of sheet in excel file
foreach ($reader->getSheetIterator() as $sheet) {
// Number of Rows in Excel sheet
foreach ($sheet->getRowIterator() as $row) {
// It reads data after header. In the my excel sheet,
// header is in the first row.
if ($count1 > 1) {
// Data of excel sheet
$data['Name'] = $row[0];
$data['Address'] = $row[1];
$data['Number'] = $row[2];
//Here, You can insert data into database.
//print_r($data);
//$sql = mysqli_query($connection, "INSERT INTO address(Name, Address, Number) VALUES ('$row[0]', '$row[1]', '$row[2]')");
foreach ($data as $key => $value) {
$value = trim($value);
if (empty($value)){
echo $output = "Row ".$count1." Column ".$key." is empty <br/>";
}
}
}
$count1++;
}
}
我有一个问题,我需要循环所有数组并在我设法做的每一列上找到缺失的行,现在我需要忽略或计算行上的重复值。我尝试做 array_unique 但它不适用于我正在使用的数组。
我也尝试将我的数组插入另一个数组,但我也遇到了插入问题。我可能在声明我的数组时遇到问题,但我仍然遇到一些问题。如何忽略多个不同数组上的重复项,或者如何将所有单独的数组合并到一个 foreach 循环内的一个数组中?对不起,我对 php 中的数组有点陌生,如果有人正在阅读这篇文章,谢谢。
编辑:我的目标是计算 xlsx 文件中数组/行中缺失值的数量。这就像示例代码中的 foreach 循环一样。示例:
Row 213 Column Name is empty
Row 214 Column Name is empty
Row 214 Column Address is empty
这就是我在 xlsx 文件的行中回显缺失值时的样子。
现在我需要验证 xlsx 文件是否有重复行。我最初的想法是要么回显具有重复值的行,即
Row 213 Column Name is has duplicates
Row 214 Column Name is has duplicates
Row 214 Column Address is empty
但是一切都变得复杂了,这导致我尝试使用 array_unique,但也遇到了问题,因为 xlsx 文件呈现数组的方式。
【问题讨论】:
-
您要寻找的输出/结果是什么?你能举个例子吗?您应该考虑使用准备好的语句,而不是将数据直接粘贴到 SQL 查询中。如果我没记错的话,
getRowIterator可以让您指定起始行,因此您不必使用$count1。 -
您好,感谢您的回复。我用一个例子编辑了这个问题。