<?php

$data = [
	[
		'id' => 2,
		'pId' => 1,
		'name' => 'xxx',
		'url' => 'www.baidu.com',
		'xxx' => 'xxx'

	],
	[
		'id' => 3,
		'pId' => 1,
		'name' => '3 name',
		'url' => 'www.baidu.com/3',
		'xxx' => 'xxx3'
	],
	[
		'id' => 4,
		'pId' => 2,
		'name' => '4 name',
		'url' => 'www.baidu.com/4',
		'xxx' => 'xxx4'
	],
	[
		'id' => 10,
		'pId' => 2,
		'name' => '10 name',
		'url' => 'www.baidu.com/10',
		'xxx' => 'xxx10'
	],
	[
		'id' => 5,
		'pId' => 3,
		'name' => '5 name',
		'url' => 'www.baidu.com/5',
		'xxx' => 'xxx5'
	],
	[
		'id' => 6,
		'pId' => 4,
		'name' => '6 name',
		'url' => 'www.baidu.com/6',
		'xxx' => 'xxx6'
	],
	[
		'id' => 7,
		'pId' => 1,
		'name' => '7 name',
		'url' => 'www.baidu.com/7',
		'xxx' => 'xxx7'
	],
];

//组成无限级分类
function getTree($data,$idName = 'id', $pIdName = 'pId', $childrenName = 'children') {
    $items = array_column($data,null, $idName);
    $tree = array();
    foreach($items as $k => $item){
        if(isset($items[$item[$pIdName]])){
            $items[$item[$pIdName]][$childrenName][$k] = &$items[$k];
        }else{
            $tree[] = &$items[$k];
        }
    }
    return $tree;

}

//获取所有分类id路径数组
function getTreeLevelArray($data, $idName = 'category_id', $childName = 'child', $arr = [], $level = 0)
{
    static $treeListArray;
    $treeListArray = $treeListArray ?? [];
    foreach ($data as $key => $val) {
        if ($level == 0) {
        	$arr = [];
        }else{
        	$arr = array_slice($arr,0,$level);
        }
        unset($data[$key]);
        $arrN = $arr;
        array_push($arr, $val[$idName]);
        if (!empty($val[$childName])) {
            $level++;
            getTreeLevelArray($val[$childName], $idName, $childName, $arr, $level);
        } else {
            $treeListArray[] = $arr;
            $arr = $arrN;
            return getTreeLevelArray($data, $idName, $childName, $arr, $level);
        }
        $level--;
    }
    return $treeListArray;
}


//获取指定分类id的一个最低级路径数据
function getCategoryPatch($CategoryIds, $CategoryWithParentIds, $categories, $categoriesTreeLevelArray){
    //根据postid找到分类id
    $catePosition = [];
    $catePosition_ids = [];
    foreach ($CategoryIds as $v){
        foreach ($categoriesTreeLevelArray as $key=>$value){
            if (in_array($v,$value)){
                $catePositionIds[$key]=$v;
                $catePosition[$key] = array_search($v,$value);
            }
        }
    }
    //定位找到最大值
    $place =array_search(max($catePosition),$catePosition);
    $patchCategoryIds =[];
    if (isset($categoriesTreeLevelArray[$place])){
        $length =  array_search($catePositionIds[$place],$categoriesTreeLevelArray[$place])+1;
        $patchCategoryIds =  array_slice($categoriesTreeLevelArray[$place],0,$length); //取从数组开始到自身的值
    }    
    $patchCategory =[];
    foreach ($patchCategoryIds as $k=>$v){
        if (isset($categories[$v])){
        	foreach ($categories[$v] as $key => $value) {
        		$patchCategory[$k][$key] = $categories[$v][$key];
        	}
        }
    }
  return $patchCategory;
}



function getCategoriesTreeLevelArray($data, $idName = 'id', $pIdName = 'pId', $childrenName = 'children'){
	$data = getTree($data, $idName = 'id', $pIdName = 'pId', $childrenName = 'children');
	return getTreeLevelArray($data, $idName = 'id',$childName='children');
}

echo '测试数组';
p($data);

echo '组成无限级分类';
p(getTree($data));


$categoriesTreeLevelArray = getCategoriesTreeLevelArray($data);
echo '获取所有分类id路径数组';
p($categoriesTreeLevelArray);


$CategoryIds = [2,10,6,5,7]; //内容所属分类id
echo '内容所属分类id';
p($CategoryIds);


$CategoryWithParentIds = array_column($data,'pId','id'); //分类id对应的父级id  [2=>1,10=>2,6=>4,5=>3,7=>1]
echo '分类id对应的父级id';
p($CategoryWithParentIds);


$categories = array_column($data,null,'id');
echo '所有分类的数据';
p($categories);



$categoryPatch = getCategoryPatch($CategoryIds, $CategoryWithParentIds, $categories, $categoriesTreeLevelArray);

echo '获取分类id中一个最低级路径数据';
p($categoryPatch);




function p($data, $die=0)
{
	echo '<br/>';
    echo "<pre>";
    print_r($data);
    echo "</pre>";
    echo '<br/>';
    if ($die) die;
}
?>

除非注明, askADC博客 的文章均为原创,转载请注明作者和出处。
本文链接地址: http://blog.askadc.com/php-%e6%97%a0%e9%99%90%e7%ba%a7%e5%88%86%e7%b1%bb%e7%9a%84%e7%bb%84%e6%88%90%e5%92%8c%e8%8e%b7%e5%8f%96%e5%86%85%e5%ae%b9%e6%89%80%e5%b1%9e%e5%88%86%e7%b1%bb%e4%b8%ad%e6%9c%80%e4%bd%8e%e4%b8%80/

发表评论

邮箱地址不会被公开。 必填项已用*标注