php - 将电子表格解析为 PHP 数组并返回具有父子关系的嵌套 MLM 表
问题描述
我目前正在做一个项目,该项目需要我上传一个电子表格文件(XLSX、XLS 等),将其放入一个数组中,然后根据父子关系,构建一个结构化的 HTML 表以显示 MLM 推荐设置.
例如:
First Name | Last Name | Parent | Child
John Johnson 0 1
Mary Sue 1 2
Harold Fineberg 1 3
Gerald George 2 4
(抱歉,解释这个的蹩脚结构)
我想显示谁是某人的孩子,他们的信息放在相对于父母的表格中。
我曾尝试使用 PhpSpreadsheet 但无济于事。我没有使用数据库,因为这将是一个简单的上传电子表格并将其显示在表格中,并将相应的关系分组在一起。
任何帮助将不胜感激,因为这是我第一次发帖,也是第一次传销树/excel项目!
<?php
require 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
if(isset($_POST['upload'])) {
$filename = $_FILES['sheet']['tmp_name'];
//$data = csv_to_array($filename);
$spreadsheet = PhpOffice\PhpSpreadsheet\IOFactory::load($filename);
$worksheet = $spreadsheet->getActiveSheet();
$rows = [];
foreach ($worksheet->getRowIterator() AS $row) {
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(FALSE); // This loops through all cells,
$cells = [];
foreach ($cellIterator as $cell) {
$cells[] = $cell->getValue();
}
$rows[] = $cells;
}
// echo '<pre>';
// print_r($rows);
// echo '</pre>';
// $output = fopen('test.xlsx', 'w');
// foreach ($rows as $file) {
// $result = [];
// array_walk_recursive($file, function($item) use (&$result) {
// $result[] = $item;
// });
// }
// $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($result, "Xlsx");
// $writer->save("05featuredemo.xlsx");
function buildTree(array $elements, $parentId = 0) {
$branch = array();
foreach ($elements as $element) {
if ($element[0] == $parentId) {
$children = buildTree($elements, $element[0]);
if ($children) {
$element[1] = $children;
}
$branch[] = $element;
}
}
return $branch;
}
$tree = buildTree($rows);
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Page Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div class="form-container">
<form enctype="multipart/form-data" method="POST">
<div class="form-box">
<input type="file" name="sheet" id="sheet" accept=".xls,.xlsx">
</div>
<div align="center">
<input type="submit" name="upload" value="Upload & Convert">
</div>
</form>
</div>
</body>
</html>
这就是我一直在使用的。我已经找到了如何将它很好地放入数组中,但我似乎无法将其转换为线性树视图:
Parent
Child
Child
Child
Child
Child
Child
注释代码是我尝试过但由于可能有用而没有删除的东西。如果我不需要使用 PhpSpreadhseet 那就太好了,但我可以妥协。
解决方案
这里有一些non_recursive代码让你开始(如果你还没有解决它),它将基于$rows
从电子表格加载的数组构建一个树。
这个想法是每个节点都有一个名称和一个子数组。. 所以代码只是在第 1 步中为每个人(父母和孩子)创建一个节点,然后在第 2 步中从下往上填充链接。
代码不健壮,如果 的行$rows
重复,列表中的顺序不正确,或者在错误的位置显示子和父,则可能会产生不需要的结果!好的生产代码需要处理这些可能性,可能通过$rows
在构建树之前检查和修复。
$rows = [];
$rows[] = array(0,1);
$rows[] = array(1,2);
$rows[] = array(1,3);
$rows[] = array(1,4);
$rows[] = array(2,5);
$rows[] = array(3,6);
$rows[] = array(6,7);
// Build the required tree
$tree = makeTree($rows);
print_r($tree);
function makeTree(array $rows){
//----------------------
// Step 1. Make a list of nodes
// -----------------------
// make the parent node
$nodeList =[];
$nodeList[0]['name'] = "parent:";
$nodeList[0]['Children'] = [];
// make the child nodes
foreach ($rows as $cells)
{
$nodeList[$cells[1]]['name'] = "child:".$cells[1];
$nodeList[$cells[1]]['Children'] = [];
}
//----------------------
// Step 2. link each child node to its parent node
// -----------------------
for ($n = count($rows)-1; $n>=0; $n--)
{ // do this from the bottom up
$nodeParent = &$nodeList[$rows[$n][0]];
$nodeChild = &$nodeList[$rows[$n][1]];
$nodeParent['Children'][$rows[$n][1]]= $nodeChild;
}
// pick out the parent node (which by now should have all links in place)
$tree[0] = $nodeList[0];
return($tree);
}
它的输出如下,可能接近或不接近您的需要。
Array
(
[0] => Array
(
[name] => parent:
[Children] => Array
(
[1] => Array
(
[name] => child:1
[Children] => Array
(
[4] => Array
(
[name] => child:4
[Children] => Array
(
)
)
[3] => Array
(
[name] => child:3
[Children] => Array
(
[6] => Array
(
[name] => child:6
[Children] => Array
(
[7] => Array
(
[name] => child:7
[Children] => Array
(
)
)
)
)
)
)
[2] => Array
(
[name] => child:2
[Children] => Array
(
[5] => Array
(
[name] => child:5
[Children] => Array
(
)
)
)
)
)
)
)
)
)
推荐阅读
- html - 多个单选按钮不起作用,只有第一个
- elasticsearch - 如果在 [basic] 许可证上启用了安全性,则必须启用传输 SSL
- visual-studio-code - 在 VS 代码上使用 sage 时如何获得 python 配色方案?
- python - 如何以不同的方式评估符号表达式?
- python-3.x - Pycrypto的CBC模式AES加密问题
- ruby - `bundle exec rake release` 和 `gem push` 有什么区别?
- ruby-on-rails - Rails 表单不保存关联属性
- node.js - 如何根据 JSON 数据呈现多个部分?
- java - Play商店发布的应用程序上的“网络无法访问”,但调试版本有效
- java - 字符串索引超出范围:-5