Skip to content

Instantly share code, notes, and snippets.

@suconghou
Last active April 23, 2017 09:37
Show Gist options
  • Save suconghou/82c8c4b60bf0d667e4aa45474a2be1ff to your computer and use it in GitHub Desktop.
Save suconghou/82c8c4b60bf0d667e4aa45474a2be1ff to your computer and use it in GitHub Desktop.
2个无限级分类
<?php
public static function data($items)
{
$items=array_column($items,null,'id');
$tree = [];
foreach($items as $item)
{
if(isset($items[$item['pid']]))
{
$items[$item['pid']]['child'][] = &$items[$item['id']];
}
else
{
$tree[] = &$items[$item['id']];
}
}
return ['num'=>count($tree),'data'=>$tree];
}
public static function data2($items)
{
$tree=[]; //最终生成的结果
$path=[]; //记录如何找到我
foreach ($items as $item)
{
$id=$item['id'];
$pid=$item['pid'];
if(isset($path[$pid])) //可以追踪到他的上一级
{
$ppath=$path[$pid];///这是他父类所在的路径结构
eval($ppath.";");
$p['child'][$id]=$item;
$path[$id]=$ppath."['child'][{$id}]";
}
else
{
$path[$id]='$p=&$tree'."[$id]";
$tree[$id]=$item;
}
}
return ['num'=>count($tree),'data'=>$tree];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment