編輯: @Pontus Carme解決方案解決了這個問題。
可以說我有多個檔案目錄和檔案字串......(檔案夾結構是動態的,它不應該遵循靜態模式)
Reference how folder structure looks:
- Root folder: $files = 'File1.doc, File2.doc';
- Subfolder: $files = 'Subfolder/File1.doc';
- Mixed: $files = 'File1.doc,
Subfolder/File1.doc,
Subfolder/Subsubfolder/File1.doc,
File2.doc';
etc.
---------
Back to code:
$files = '2021/Dec/File1.doc,
2021/Dec/File2.doc,
2021/Dec/File3.doc,
2021/Nov/File1.doc,
2021/Nov/File2.doc,
2021/Nov/File3.doc,
2021/Nov/File4.doc,
2020/Jan/File1.doc,
2020/Jan/File2.doc,
2020/Jan/File3.doc';
// Make it as an array
$files = explode(',', $files);
...首先我想拆分每個檔案字串并提取目錄和檔案
$data = [];
foreach($files as $key => $file)
{
$separator = explode('/', $file); // Output: [0 => '2021', 1 => 'Dec', 2 => 'File1.doc'];
$file_name = end($separator); // Get file name as last element
array_pop($separator); // Remove file name from array
$files = [];
$files[] = [
'name' => $file_name,
'folder' => $separator,
];
$node = [];
foreach ($files as $row)
{
$node['id'] = $key;
// This is main folder (root)
if (empty($row['folder']))
{
$node['file'] = $row['name'];
$node['parent_id'] = 0;
}
else
{
// THIS IS PROBLEMATIC HOW TO NOW GET PARENT ID AND FOLDER NAME
foreach ($row['folder'] as $fkey => $frow)
{
// I NEED HERE A PARENT_ID FROM PREVIOUS CHILD AND FILE NAME OF CURRENT FOLDER
}
}
}
$data[] = $node;
}
...現在我需要獲取檔案夾名稱并檢查檔案夾是否有子檔案夾并設定 parent_id = 檔案夾 ID。
...如何從上面的 foreach 回圈中生成這個陣列結構?
[
[
'id' => 1,
'file' => '2021',
'parent_id' => 0, // Main folder (2021) (NOT ROOT, just first folder generated in directory)
],
[
'id' => 2,
'file' => 'Dec',
'parent_id' => 1, // Child of 2021
],
[
'id' => 3,
'file' => 'File1.doc',
'parent_id' => 2, // Child of Dec
],
[
'id' => 4,
'file' => 'File2.doc',
'parent_id' => 2, // Child of Dec
],
[
'id' => 5,
'file' => 'File3.doc',
'parent_id' => 2, // Child of Dec
],
[
'id' => 6,
'file' => 'Nov',
'parent_id' => 1, // Child of 2021
],
[
'id' => 7,
'file' => 'File1.doc',
'parent_id' => 6, // Child of Nov
],
[
'id' => 8,
'file' => 'File2.doc',
'parent_id' => 6, // Child of Nov
],
[
'id' => 9,
'file' => 'Fil3.doc',
'parent_id' => 6, // Child of Nov
],
[
'id' => 10,
'file' => 'File4.doc',
'parent_id' => 6, // Child of Nov
],
[
'id' => 11,
'file' => '2020',
'parent_id' => 0, // Main folder (2020) (NOT ROOT, just second main folder generated in directory)
],
[
'id' => 12,
'file' => 'Jan',
'parent_id' => 11, // Child of 2020
],
[
'id' => 13,
'file' => 'File1.doc',
'parent_id' => 11, // Child of jan
],
[
'id' => 14,
'file' => 'File2.doc',
'parent_id' => 11, // Child of jan
],
[
'id' => 15,
'file' => 'File3.doc',
'parent_id' => 11, // Child of jan
],
];
這是最終結構的外觀以及我計劃如何使用遞回函式來獲取樹級陣列。
https://3v4l.org/6ULBZ
uj5u.com熱心網友回復:
希望這將在按層次結構管理資料后為您提供所需的輸出。此代碼不僅對您的路徑是靜態的,而且是動態的,因此如果您將在 hirarchey 中添加更多檔案夾,或者如果您從路徑中洗掉一些檔案夾,那么此代碼也可以正常作業。
<?php
$files = '
2021/Dec/File1.doc,
2021/Dec/File2.doc,
2021/Dec/File3.doc,
2021/Nov/File1.doc,
2021/Nov/File2.doc,
2021/Nov/File3.doc,
2021/Nov/File4.doc,
2020/Jan/File1.doc,
2020/Jan/File2.doc,
2020/Jan/File3.doc';
// Make it as an array
$files = explode(',', $files);
$data = [];
$autoIncreamentId = 0;
$level = [];
$parent_ids = [];
foreach($files as $key => $file)
{
$separator = explode('/', $file); // Output: [0 => '2021', 1 => 'Dec', 2 => 'File1.doc'];
$node = [];
foreach ($separator as $ind=>$row)
{
$row = trim($row);
$isDir = 1;
if (preg_match('/[^\/]*\.(doc|txt)/', $row, $o))
{
$isDir = 0;
}
//if directory already used then just pull id for child use
if($isDir && isset($level[$ind.'_'.$row])){
//maintain ids of the parent hirachy to use in child files
$node[$ind] = array(
'id'=>(int)$level[$ind.'_'.$row]
);
}else{
//add node using previous level id
$autoIncreamentId =1;
$parent_id = isset($node[$ind-1]['id'])? (int)$node[$ind-1]['id']:0;
$node[$ind] = array(
'id'=>$autoIncreamentId,
'file'=> $row,
'parent_id' => $parent_id
);
//save level wise id to use in future
$level[$ind.'_'.$row] = $autoIncreamentId;
}
}
//add in data just node and ignore empty nodes
foreach($node as $item){
if(isset($item['file'])){
$data[] = $item;
}
}
}
print_r(json_encode($data));
輸出:
[
{
"id": 1,
"file": "2021",
"parent_id": 0
},
{
"id": 2,
"file": "Dec",
"parent_id": 1
},
{
"id": 3,
"file": "File1.doc",
"parent_id": 2
},
{
"id": 4,
"file": "File2.doc",
"parent_id": 2
},
{
"id": 5,
"file": "File3.doc",
"parent_id": 2
},
{
"id": 6,
"file": "Nov",
"parent_id": 1
},
{
"id": 7,
"file": "File1.doc",
"parent_id": 6
},
{
"id": 8,
"file": "File2.doc",
"parent_id": 6
},
{
"id": 9,
"file": "File3.doc",
"parent_id": 6
},
{
"id": 10,
"file": "File4.doc",
"parent_id": 6
},
{
"id": 11,
"file": "2020",
"parent_id": 0
},
{
"id": 12,
"file": "Jan",
"parent_id": 11
},
{
"id": 13,
"file": "File1.doc",
"parent_id": 12
},
{
"id": 14,
"file": "File2.doc",
"parent_id": 12
},
{
"id": 15,
"file": "File3.doc",
"parent_id": 12
}
]
如果 $files = 'File1.doc, File2.doc,Folder/File3.doc';
Array
(
[0] => Array
(
[id] => 1
[file] => File1.doc
[parent_id] => 0
)
[1] => Array
(
[id] => 2
[file] => File2.doc
[parent_id] => 0
)
[2] => Array
(
[id] => 3
[file] => Folder
[parent_id] => 0
)
[3] => Array
(
[id] => 4
[file] => File3.doc
[parent_id] => 3
)
)
uj5u.com熱心網友回復:
我重寫了代碼,但這可以解決問題:
<?php
$files = '2021/Dec/File1.doc,
2021/Dec/File2.doc,
2021/Dec/File3.doc,
2021/Nov/File1.doc,
2021/Nov/File2.doc,
2021/Nov/File3.doc,
2021/Nov/File4.doc,
2020/Jan/File1.doc,
2020/Jan/File2.doc,
2020/Jan/File3.doc';
// Make it as an array
$files = explode(",\n", $files);
$out = [];
foreach ($files as $line) {
[$year, $month, $file] = explode('/', $line);
if (!$year_id = get_id($out, $year)) {
$year_id = add_value($out, $year);
}
if (!$month_id = get_id($out, $month)) {
$month_id = add_value($out, $month, $year_id);
}
add_value($out, $file, $month_id);
}
var_dump($out);
function add_value(&$out, $name, $parent_id = null)
{
$last_id = end($out)['id'] ?? 0;
$new_id = $last_id 1;
array_push($out, [
'id' => $new_id,
'file' => $name,
'parent_id' => $parent_id ?? 0,
]);
return $new_id;
}
function get_id($out, $name)
{
foreach ($out as $item) {
if ($item['file'] == $name) {
return $item['id'];
}
}
return null;
}
->
[
{
"id": 1,
"file": "2021",
"parent_id": 0
},
{
"id": 2,
"file": "Dec",
"parent_id": 1
},
{
"id": 3,
"file": "File1.doc",
"parent_id": 2
},
{
"id": 4,
"file": "File2.doc",
"parent_id": 2
},
{
"id": 5,
"file": "File3.doc",
"parent_id": 2
},
{
"id": 6,
"file": "Nov",
"parent_id": 1
},
{
"id": 7,
"file": "File1.doc",
"parent_id": 6
},
{
"id": 8,
"file": "File2.doc",
"parent_id": 6
},
{
"id": 9,
"file": "File3.doc",
"parent_id": 6
},
{
"id": 10,
"file": "File4.doc",
"parent_id": 6
},
{
"id": 11,
"file": "2020",
"parent_id": 0
},
{
"id": 12,
"file": "Jan",
"parent_id": 11
},
{
"id": 13,
"file": "File1.doc",
"parent_id": 12
},
{
"id": 14,
"file": "File2.doc",
"parent_id": 12
},
{
"id": 15,
"file": "File3.doc",
"parent_id": 12
}
]
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/407631.html
標籤:
