-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.php
More file actions
40 lines (36 loc) · 1.18 KB
/
Copy pathparse.php
File metadata and controls
40 lines (36 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php
// Max Base
// https://github.com/BaseMax/ShopCategories
$data = file_get_contents("categories.txt");
$lines = explode("\n", trim($data));
$categories = [];
$main_parent_id = null;
$parent_id = null;
foreach($lines as $i => $line) {
// if($i > 20) break;
$name = trim($line);
if($name === "") continue;
if(str_starts_with($name, "**")) {
$name = trim(str_replace("**", "", $name));
$main_parent_id = count($categories);
// print "Create $main_parent_id main category\n";
$categories[$main_parent_id] = [
"name" => $name,
"childs" => [],
];
} else if(str_starts_with($name, "==")) {
$name = trim(str_replace("==", "", $name));
$parent_id = count($categories[$main_parent_id]["childs"]);
// print "Create $main_parent_id father category\n";
$categories[$main_parent_id]["childs"][$parent_id] = [
"name" => $name,
"childs" => [],
];
} else {
$categories[$main_parent_id]["childs"][$parent_id]["childs"][] = [
"name" => $name,
];
}
}
print_r($categories);
file_put_contents("categories.json", json_encode($categories));