<?php
$sourceArray = array(
array('ID' => 705, 'NAME' => 'Телефоны и гаджеты', 'DEPTH_LEVEL' => 1),
array('ID' => 706, 'NAME' => 'Смартфоны', 'DEPTH_LEVEL' => 2),
array('ID' => 5586, 'NAME' => 'Аксессуары для гаджетов', 'DEPTH_LEVEL' => 2),
array('ID' => 715, 'NAME' => 'Аксессуары для телефонов', 'DEPTH_LEVEL' => 3),
array('ID' => 716, 'NAME' => 'Чехлы для смартфонов', 'DEPTH_LEVEL' => 4),
array('ID' => 5536, 'NAME' => 'Чехлы для Apple', 'DEPTH_LEVEL' => 5),
array('ID' => 5539, 'NAME' => 'Чехлы для Samsung', 'DEPTH_LEVEL' => 5),
array('ID' => 3010, 'NAME' => 'Защитные стекла для телефонов', 'DEPTH_LEVEL' => 3),
array('ID' => 660, 'NAME' => 'Компьютерная техника', 'DEPTH_LEVEL' => 1)
);
$sourceArray2 = array(
array('ID' => 705, 'NAME' => 'Телефоны и гаджеты', 'DEPTH_LEVEL' => 1),
array('ID' => 706, 'NAME' => 'Смартфоны', 'DEPTH_LEVEL' => 2),
array('ID' => 5586, 'NAME' => 'Аксессуары для гаджетов', 'DEPTH_LEVEL' => 2),
array('ID' => 715, 'NAME' => 'Аксессуары для телефонов', 'DEPTH_LEVEL' => 3),
array('ID' => 716, 'NAME' => 'Чехлы для смартфонов', 'DEPTH_LEVEL' => 4),
array('ID' => 5536, 'NAME' => 'Чехлы для Apple', 'DEPTH_LEVEL' => 5),
array('ID' => 5539, 'NAME' => 'Чехлы для Samsung', 'DEPTH_LEVEL' => 5),
array('ID' => 3010, 'NAME' => 'Защитные стекла для телефонов', 'DEPTH_LEVEL' => 3),
array('ID' => 660, 'NAME' => 'Компьютерная техника', 'DEPTH_LEVEL' => 1),
array('ID' => 660, 'NAME' => 'Компьютерная техника', 'DEPTH_LEVEL' => 2),
array('ID' => 660, 'NAME' => 'Компьютерная техника', 'DEPTH_LEVEL' => 1),
array('ID' => 660, 'NAME' => 'Компьютерная техника', 'DEPTH_LEVEL' => 2),
array('ID' => 660, 'NAME' => 'Компьютерная техника', 'DEPTH_LEVEL' => 2),
array('ID' => 660, 'NAME' => 'Компьютерная техника', 'DEPTH_LEVEL' => 1),
);
$expect = [
[
'ID' => 705,
'NAME' => 'Телефоны и гаджеты',
'DEPTH_LEVEL' => 1,
'CHILD' => [
[
'ID' => 706,
'NAME' => 'Смартфоны',
'DEPTH_LEVEL' => 2
],
[
'ID' => 5586,
'NAME' => 'Аксессуары для гаджетов',
'DEPTH_LEVEL' => 2,
'CHILD' => [
[
'ID' => 715,
'NAME' => 'Аксессуары для телефонов',
'DEPTH_LEVEL' => 3,
'CHILD' => [
[
'ID' => 716,
'NAME' => 'Чехлы для смартфонов',
'DEPTH_LEVEL' => 4,
'CHILD' => [
[
'ID' => 5536,
'NAME' => 'Чехлы для Apple',
'DEPTH_LEVEL' => 5
],
[
'ID' => 5539,
'NAME' => 'Чехлы для Samsung',
'DEPTH_LEVEL' => 5
]
]
]
]
],
[
'ID' => 3010,
'NAME' => 'Защитные стекла для телефонов',
'DEPTH_LEVEL' => 3
]
]
]
]
],
[
'ID' => 660,
'NAME' => 'Компьютерная техника',
'DEPTH_LEVEL' => 1
]
];
$expect2 = [
[
'ID' => 705,
'NAME' => 'Телефоны и гаджеты',
'DEPTH_LEVEL' => 1,
'CHILD' => [
[
'ID' => 706,
'NAME' => 'Смартфоны',
'DEPTH_LEVEL' => 2
],
[
'ID' => 5586,
'NAME' => 'Аксессуары для гаджетов',
'DEPTH_LEVEL' => 2,
'CHILD' => [
[
'ID' => 715,
'NAME' => 'Аксессуары для телефонов',
'DEPTH_LEVEL' => 3,
'CHILD' => [
[
'ID' => 716,
'NAME' => 'Чехлы для смартфонов',
'DEPTH_LEVEL' => 4,
'CHILD' => [
[
'ID' => 5536,
'NAME' => 'Чехлы для Apple',
'DEPTH_LEVEL' => 5
],
[
'ID' => 5539,
'NAME' => 'Чехлы для Samsung',
'DEPTH_LEVEL' => 5
]
]
]
]
],
[
'ID' => 3010,
'NAME' => 'Защитные стекла для телефонов',
'DEPTH_LEVEL' => 3
]
]
]
]
],
[
'ID' => 660,
'NAME' => 'Компьютерная техника',
'DEPTH_LEVEL' => 1,
'CHILD' => [
array('ID' => 660, 'NAME' => 'Компьютерная техника', 'DEPTH_LEVEL' => 2)
]
],
[
'ID' => 660,
'NAME' => 'Компьютерная техника',
'DEPTH_LEVEL' => 1,
'CHILD' => [
array('ID' => 660, 'NAME' => 'Компьютерная техника', 'DEPTH_LEVEL' => 2),
array('ID' => 660, 'NAME' => 'Компьютерная техника', 'DEPTH_LEVEL' => 2)
]
],
array('ID' => 660, 'NAME' => 'Компьютерная техника', 'DEPTH_LEVEL' => 1),
];
/* ----Рекурсивный кусок говна---- */
// function makeTree(array $arr){
// $parent_lvl = -1;
// $parent_index = -1;
// $res = [];
// foreach($arr as $index => $sub) {
// $lvl = $sub['DEPTH_LEVEL'];
// if ($index === 0 || $lvl === $parent_lvl) {
// $parent_index++;
// $res[$parent_index] = array_merge($sub, ['CHILD' => []] );
// $parent_lvl = $lvl;
// } elseif($lvl > $parent_lvl) {
// $res[$parent_index]['CHILD'][] = $sub;
// }
// }
// for($i = 0; $i < count($res); $i++ ){
// $res[$i]['CHILD'] = makeTree($res[$i]['CHILD']);
// }
// return $res;
// }
// function removeEmptyNodes(array $arr) {
// $count = count($arr);
// for($i = 0; $i < $count; $i++){
// $hasChildNode = isset($arr[$i]['CHILD']);
// $ChildNodeIsNotEmpty = $hasChildNode && count($arr[$i]['CHILD']) > 0;
// if($ChildNodeIsNotEmpty){
// $arr[$i]['CHILD'] = removeEmptyNodes($arr[$i]['CHILD']);
// } elseif ($hasChildNode) {
// unset($arr[$i]['CHILD']);
// }
// }
// return $arr;
// }
// $res = removeEmptyNodes(makeTree($sourceArray));
// $res2 = removeEmptyNodes(makeTree($sourceArray2));
// test($res, $expect); echo "\n";
// test($res2, $expect2); echo "\n";
// mijikaiDump($res); echo "\n";
// mijikaiDump($res2); echo "\n";
// var_dump($res); echo "\n\n\n\n\n";
// var_dump($res2);
/* ----Рекурсивный кусок говна---- */
/* ----Нерекурсивный сладкий рулет---- */
function makeTreeIter(array $arr){
$parents = []; // level => index
$res = [];
foreach($arr as $index=>$sub){
$lvl = $sub['DEPTH_LEVEL'];
if(isset($parents[$lvl-1])){
$i = &$parents[$lvl-1];
$i['CHILD'][] = $sub;
$lastIndex = count($i['CHILD'])-1;
$ref = &$i['CHILD'][$lastIndex];
$parents[$lvl] = &$ref;
} else {
$res[] = $sub;
$lastIndex = count($res)-1;
$ref = &$res[$lastIndex];
$parents[$lvl] = &$ref;
}
}
return $res;
}
$res = makeTreeIter($sourceArray);
$res2 = makeTreeIter($sourceArray2);
test($res, $expect); echo "\n";
test($res2, $expect2); echo "\n";
mijikaiDump($res); echo "\n";
mijikaiDump($res2); echo "\n";
var_dump($res); echo "\n\n\n\n\n";
var_dump($res2);
/* ----Нерекурсивный сладкий рулет---- */
function test($res, $expect) {
if($res === $expect) {
echo "OK: resuling and expected arrays match.";
} else {
throw new Exception("KO: resulting and expected arrays don't match.");
}
}
function mijikaiDump(array $arr, string $tab = ''):void {
foreach($arr as $val){
echo $tab.$val['DEPTH_LEVEL']." ".$val['NAME']."\n";
if(isset($val['CHILD'])) {
mijikaiDump($val['CHILD'],$tab.' ');
}
}
}
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename: /in/OVVIA
function name: (null)
number of ops: 38
compiled vars: !0 = $sourceArray, !1 = $sourceArray2, !2 = $expect, !3 = $expect2, !4 = $res, !5 = $res2
line #* E I O op fetch ext return operands
-------------------------------------------------------------------------------------
2 0 E > ASSIGN !0, <array>
13 1 ASSIGN !1, <array>
31 2 ASSIGN !2, <array>
86 3 ASSIGN !3, <array>
244 4 INIT_FCALL 'maketreeiter'
5 SEND_VAR !0
6 DO_FCALL 0 $10
7 ASSIGN !4, $10
245 8 INIT_FCALL 'maketreeiter'
9 SEND_VAR !1
10 DO_FCALL 0 $12
11 ASSIGN !5, $12
247 12 INIT_FCALL_BY_NAME 'test'
13 SEND_VAR_EX !4
14 SEND_VAR_EX !2
15 DO_FCALL 0
16 ECHO '%0A'
248 17 INIT_FCALL_BY_NAME 'test'
18 SEND_VAR_EX !5
19 SEND_VAR_EX !3
20 DO_FCALL 0
21 ECHO '%0A'
250 22 INIT_FCALL_BY_NAME 'mijikaiDump'
23 SEND_VAR_EX !4
24 DO_FCALL 0
25 ECHO '%0A'
251 26 INIT_FCALL_BY_NAME 'mijikaiDump'
27 SEND_VAR_EX !5
28 DO_FCALL 0
29 ECHO '%0A'
253 30 INIT_FCALL 'var_dump'
31 SEND_VAR !4
32 DO_ICALL
33 ECHO '%0A%0A%0A%0A%0A'
254 34 INIT_FCALL 'var_dump'
35 SEND_VAR !5
36 DO_ICALL
274 37 > RETURN 1
Function maketreeiter:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 37
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 37
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 27
Branch analysis from position: 11
1 jumps found. (Code = 42) Position 1 = 36
Branch analysis from position: 36
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 27
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 37
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 37
filename: /in/OVVIA
function name: makeTreeIter
number of ops: 40
compiled vars: !0 = $arr, !1 = $parents, !2 = $res, !3 = $sub, !4 = $index, !5 = $lvl, !6 = $i, !7 = $lastIndex, !8 = $ref
line #* E I O op fetch ext return operands
-------------------------------------------------------------------------------------
220 0 E > RECV !0
221 1 ASSIGN !1, <array>
222 2 ASSIGN !2, <array>
224 3 > FE_RESET_R $11 !0, ->37
4 > > FE_FETCH_R ~12 $11, !3, ->37
5 > ASSIGN !4, ~12
225 6 FETCH_DIM_R ~14 !3, 'DEPTH_LEVEL'
7 ASSIGN !5, ~14
227 8 SUB ~16 !5, 1
9 ISSET_ISEMPTY_DIM_OBJ 0 !1, ~16
10 > JMPZ ~17, ->27
228 11 > SUB ~18 !5, 1
12 FETCH_DIM_W $19 !1, ~18
13 ASSIGN_REF !6, $19
229 14 FETCH_DIM_W $21 !6, 'CHILD'
15 ASSIGN_DIM $21
16 OP_DATA !3
230 17 FETCH_DIM_R ~23 !6, 'CHILD'
18 COUNT ~24 ~23
19 SUB ~25 ~24, 1
20 ASSIGN !7, ~25
231 21 FETCH_DIM_W $27 !6, 'CHILD'
22 FETCH_DIM_W $28 $27, !7
23 ASSIGN_REF !8, $28
232 24 FETCH_DIM_W $30 !1, !5
25 ASSIGN_REF $30, !8
227 26 > JMP ->36
234 27 > ASSIGN_DIM !2
28 OP_DATA !3
235 29 COUNT ~33 !2
30 SUB ~34 ~33, 1
31 ASSIGN !7, ~34
236 32 FETCH_DIM_W $36 !2, !7
33 ASSIGN_REF !8, $36
237 34 FETCH_DIM_W $38 !1, !5
35 ASSIGN_REF $38, !8
224 36 > > JMP ->4
37 > FE_FREE $11
240 38 > RETURN !2
241 39* > RETURN null
End of function maketreeiter
Function test:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 6
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 10
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 6
1 jumps found. (Code = 108) Position 1 = -2
filename: /in/OVVIA
function name: test
number of ops: 11
compiled vars: !0 = $res, !1 = $expect
line #* E I O op fetch ext return operands
-------------------------------------------------------------------------------------
259 0 E > RECV !0
1 RECV !1
260 2 IS_IDENTICAL !0, !1
3 > JMPZ ~2, ->6
261 4 > ECHO 'OK%3A+resuling+and+expected+arrays+match.'
260 5 > JMP ->10
263 6 > NEW $3 'Exception'
7 SEND_VAL_EX 'KO%3A+resulting+and+expected+arrays+don%27t+match.'
8 DO_FCALL 0
9 > THROW 0 $3
265 10 > > RETURN null
End of function test
Function mijikaidump:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 21
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 21
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 13, Position 2 = 20
Branch analysis from position: 13
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 20
Branch analysis from position: 21
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 21
filename: /in/OVVIA
function name: mijikaiDump
number of ops: 23
compiled vars: !0 = $arr, !1 = $tab, !2 = $val
line #* E I O op fetch ext return operands
-------------------------------------------------------------------------------------
267 0 E > RECV !0
1 RECV_INIT !1 ''
268 2 > FE_RESET_R $3 !0, ->21
3 > > FE_FETCH_R $3, !2, ->21
269 4 > FETCH_DIM_R ~4 !2, 'DEPTH_LEVEL'
5 CONCAT ~5 !1, ~4
6 CONCAT ~6 ~5, '+'
7 FETCH_DIM_R ~7 !2, 'NAME'
8 CONCAT ~8 ~6, ~7
9 CONCAT ~9 ~8, '%0A'
10 ECHO ~9
270 11 ISSET_ISEMPTY_DIM_OBJ 0 !2, 'CHILD'
12 > JMPZ ~10, ->20
271 13 > INIT_FCALL_BY_NAME 'mijikaiDump'
14 CHECK_FUNC_ARG
15 FETCH_DIM_FUNC_ARG $11 !2, 'CHILD'
16 SEND_FUNC_ARG $11
17 CONCAT ~12 !1, '+++'
18 SEND_VAL_EX ~12
19 DO_FCALL 0
268 20 > > JMP ->3
21 > FE_FREE $3
274 22 > RETURN null
End of function mijikaidump
Generated using Vulcan Logic Dumper, using php 8.0.0
preferences:
137.93 ms | 1013 KiB | 16 Q