Skip to content

Instantly share code, notes, and snippets.

@alexsunxl
Last active February 28, 2017 10:30
Show Gist options
  • Save alexsunxl/4053be759cbf7ea6e2d18b78022c3a81 to your computer and use it in GitHub Desktop.
Save alexsunxl/4053be759cbf7ea6e2d18b78022c3a81 to your computer and use it in GitHub Desktop.
php 无限级分类,获得树形数组
<?php
// ...
public static function getCategory($pid = 0){
$tmp = Category::where('pid', $pid)->get()->toArray();
$category = [];
if (count($tmp) != 0){
foreach ($tmp as $key => $value){
// 通过递归处理获得子类
$value['sub'] = self::getCategory($value['id']);
$category[] = $value;
}
}
return $category;
}
// ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment