PHP依赖关系算法实现

2021/9/30 14:10:40

本文主要是介绍PHP依赖关系算法实现,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

/**
 * 根据依赖关系排序
 *
 * @param array $fieldFeture         事情清单
 * @param array $relationMapList     依赖关系清单
 * @param array $relationRightValues 依赖关系值清单
 *
 * @return array
 */
public static function relationSort(array $fieldFeture, array $relationMapList, array $relationRightValues)
{
    //从 事件清单 中拆分在 依赖关系值清单 中出现的数据
    $sortFieldFeature = $unSortFieldFeature = [];
    foreach ($fieldFeture as $val) {
        if (in_array($val, $relationRightValues)) {
            $unSortFieldFeature[] = $val;   //出现过的值
        } else {
            $sortFieldFeature[] = $val;     //未出现过的值
        }
    }

    //从 依赖关系 清单中删除包含上一步未出现值的关系对(即键中包含未出现值的关系对)
    unset($relationRightValues);
    foreach ($relationMapList as $key => $item) {
        foreach ($item as $ke => $val) {
            if (in_array($ke, $sortFieldFeature)) {
                unset($relationMapList[$key]);
            } else {
                $relationRightValues[] = $val;
            }
        }
    }

    if (isset($relationRightValues)) {
        $relationRightValues = array_values(array_unique($relationRightValues));
    }

    if ($relationMapList && $unSortFieldFeature) {
        $res = self::relationSort($unSortFieldFeature, $relationMapList, $relationRightValues);
        $sortFieldFeature = ArrayHelper::merge($sortFieldFeature, $res);

    } else {
        //没有关系对时,返回剩余的 出现过的值
        return ArrayHelper::merge($sortFieldFeature, $unSortFieldFeature);
    }

    return $sortFieldFeature;
}


这篇关于PHP依赖关系算法实现的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程