37 lines
1.5 KiB
PHP
37 lines
1.5 KiB
PHP
<?php
|
||
|
||
/**
|
||
* Преобразование дерева из модели Plain в массив массивов (Adjacency List)
|
||
*/
|
||
|
||
/**
|
||
* Обходит таблицу как дерево
|
||
* $fn ($name, $index, $rows, $cc)
|
||
* $name Ключ уровня
|
||
* $index Значение ключа уровня
|
||
* $rows Все столбцы текущго уровня
|
||
* $cc Столбцы более низкого уровня
|
||
*
|
||
* @param Array $level Уровни вложенности
|
||
* @param array $table Таблица
|
||
* @param Function $fn Функция которая применяется к каждой ветке дерева
|
||
*/
|
||
namespace ctiso;
|
||
use ctiso\Functions;
|
||
|
||
class TableTree {
|
||
static function walk($level, $table, $fn) {
|
||
if (empty ($level)) return $table;
|
||
$name = array_shift ($level);
|
||
|
||
$keys = Functions::key_unique_values($name, $table);
|
||
$data = [];
|
||
foreach ($keys as $index) {
|
||
list($rows, $table) = Functions::partition (Functions::lcurry(['\ctiso\Functions', '__index'], $index, $name), $table);
|
||
// $rows = array_filter ($table, lcurry('__index', intval($index), $name));
|
||
//$rows = array_filter ($table, create_function ('$x', 'return __index ('.intval($index).', \''.$name.'\', $x);'));
|
||
$data[$index] = call_user_func ($fn, $name, $index, $rows, self::walk ($level, $rows, $fn));
|
||
}
|
||
return $data;
|
||
}
|
||
}
|