-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTreeSelect.php
44 lines (37 loc) · 1.35 KB
/
TreeSelect.php
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
41
42
43
44
<?php
namespace alex290\treeselect;
use yii\helpers\ArrayHelper;
/**
* This is just an example.
*/
class TreeSelect extends \yii\base\Model {
protected function Tree($cattrees, $tab = '') {
$trees = [];
$treesChild = [];
foreach ($cattrees as $treecat) {
$value = ArrayHelper::getValue($treecat, 'name');
ArrayHelper::setValue($treecat, 'name', $tab . $value);
if (!isset($treecat['childs'])) {
$trees[] = $treecat;
} else {
$trees[] = $treecat;
foreach ($this->Tree($treecat['childs'], $tab.'-') as $t)
$trees[] = $t;
}
}
return $trees;
}
public function getTree($arrayTree) {
$tree = [];
$catstree = $arrayTree;
foreach ($catstree as $id => &$node) {
if (!$node['parent_id'])
$tree[$id] = &$node;
else
$catstree[$node['parent_id']]['childs'][$node['id']] = &$node;
}
ArrayHelper::multisort($tree, 'weight'); // Сортируем массив по полю weight. Если это необходимо
$treeOne = ArrayHelper::map($this->Tree($tree), 'id', 'name'); //Вызываем функцию Tree и преобразуем его для select
return $treeOne;
}
}