Skip to content

Instantly share code, notes, and snippets.

@jaredchu
Last active June 8, 2018 03:12
Show Gist options
  • Save jaredchu/f9657a329e8ac05b90964c85ce2cec23 to your computer and use it in GitHub Desktop.
Save jaredchu/f9657a329e8ac05b90964c85ce2cec23 to your computer and use it in GitHub Desktop.
Convert flat tree to tree
[
[
'id' => 1,
'name_lv1' => 'Parent0',
'name_lv2' => null,
'name_lv3' => null,
'name_lv4' => null,
'name_lv5' => null,
],
[
'id' => 2,
'name_lv1' => 'Parent1',
'name_lv2' => null,
'name_lv3' => null,
'name_lv4' => null,
'name_lv5' => null,
],
[
'id' => 3,
'name_lv1' => 'Parent1',
'name_lv2' => 'Childen_1_1',
'name_lv3' => null,
'name_lv4' => null,
'name_lv5' => null,
],
[
'id' => 4,
'name_lv1' => 'Parent1',
'name_lv2' => 'Childen_1_1',
'name_lv3' => 'Childen_2_1',
'name_lv4' => null,
'name_lv5' => null,
],
[
'id' => 5,
'name_lv1' => 'Parent1',
'name_lv2' => 'Childen_1_1',
'name_lv3' => 'Childen_2_1',
'name_lv4' => 'Childen_3_1',
'name_lv5' => null,
],
[
'id' => 6,
'name_lv1' => 'Parent1',
'name_lv2' => 'Childen_1_1',
'name_lv3' => 'Childen_2_1',
'name_lv4' => 'Childen_3_1',
'name_lv5' => 'Childen_4_1',
],
[
'id' => 7,
'name_lv1' => 'Parent1',
'name_lv2' => 'Childen_1_1',
'name_lv3' => 'Childen_2_1',
'name_lv4' => 'Childen_3_1',
'name_lv5' => 'Childen_4_2',
],
[
'id' => 8,
'name_lv1' => 'Parent2',
'name_lv2' => null,
'name_lv3' => null,
'name_lv4' => null,
'name_lv5' => null,
],
[
'id' => 9,
'name_lv1' => 'Parent2',
'name_lv2' => 'Childen_1_2',
'name_lv3' => null,
'name_lv4' => null,
'name_lv5' => null,
],
]
Array
(
[0] => Array
(
[id] => 1
[name_lv1] => Parent0
[name_lv2] =>
[name_lv3] =>
[name_lv4] =>
[name_lv5] =>
)
[1] => Array
(
[id] => 2
[name_lv1] => Parent1
[name_lv2] =>
[name_lv3] =>
[name_lv4] =>
[name_lv5] =>
[0] => Array
(
[id] => 3
[name_lv1] => Parent1
[name_lv2] => Childen_1_1
[name_lv3] =>
[name_lv4] =>
[name_lv5] =>
[0] => Array
(
[id] => 4
[name_lv1] => Parent1
[name_lv2] => Childen_1_1
[name_lv3] => Childen_2_1
[name_lv4] =>
[name_lv5] =>
[0] => Array
(
[id] => 5
[name_lv1] => Parent1
[name_lv2] => Childen_1_1
[name_lv3] => Childen_2_1
[name_lv4] => Childen_3_1
[name_lv5] =>
[0] => Array
(
[id] => 6
[name_lv1] => Parent1
[name_lv2] => Childen_1_1
[name_lv3] => Childen_2_1
[name_lv4] => Childen_3_1
[name_lv5] => Childen_4_1
)
[1] => Array
(
[id] => 7
[name_lv1] => Parent1
[name_lv2] => Childen_1_1
[name_lv3] => Childen_2_1
[name_lv4] => Childen_3_1
[name_lv5] => Childen_4_2
)
)
)
)
)
[2] => Array
(
[id] => 8
[name_lv1] => Parent2
[name_lv2] =>
[name_lv3] =>
[name_lv4] =>
[name_lv5] =>
[0] => Array
(
[id] => 9
[name_lv1] => Parent2
[name_lv2] => Childen_1_2
[name_lv3] =>
[name_lv4] =>
[name_lv5] =>
)
)
)
class Tree
{
public $items;
public $prefix;
/**
* Tree constructor.
* @param $items
* @param string $prefix
*/
public function __construct($items, $prefix = 'name_lv')
{
$this->items = $items;
$this->prefix = $prefix;
}
public function toBranchTree()
{
$treeWithLevel = $this->toTreeWithLevel();
$reverseLevel = count($treeWithLevel) + 1;
while (--$reverseLevel > 0) {
if ($reverseLevel !== 1) {
foreach ($treeWithLevel[$reverseLevel] as $currentLevel) {
foreach ($treeWithLevel[$reverseLevel - 1] as $index => $parentLevel) {
if (strpos($currentLevel['child_id'], $parentLevel['child_id']) !== false) {
$treeWithLevel[$reverseLevel - 1][$index][] = $currentLevel;
}
}
}
}
}
return $treeWithLevel[1];
}
public function toTreeWithLevel()
{
$treeWithLevel = [];
foreach ($this->items as $item) {
$level = $this->getItemLevel($item);
$item['child_id'] = $this->getChildId($item);
$treeWithLevel[$level][] = $item;
}
return $treeWithLevel;
}
protected function getItemLevel($item)
{
$level = 0;
foreach ($item as $key => $value) {
if (strpos($key, $this->prefix) !== false && $value != null) {
$level++;
}
}
return $level;
}
protected function getChildId($item)
{
$prefix = $this->prefix;
return array_reduce(array_keys($item), function ($carry, $key) use ($item, $prefix) {
if (strpos($key, $prefix) !== false) {
$carry .= $item[$key];
}
return $carry;
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment