【Code】Js-mind插件中会使用到的一个递归拼装数据格式的函数

js-mind插件使用效果

后续再发布js-mind插件的使用文章,这里只先提供一个会用到的函数。js-mind插件的文字框在点击后可以跳转链接,这个函数就是为了拼装链接的。
【Code】Js-mind插件中会使用到的一个递归拼装数据格式的函数_第1张图片

函数

<?php
/**
 * 递归填充Jsmind数据格式
 * @param array $arrTemp 需要处理的数组
 * @param array $arrOption 配置参数
 * @param string $arrOption['key'] 需要处理数据的字段
 * @param string $arrOption['before'] 字段值之前的的填充数据
 * @param string $arrOption['middle'] 为锚点添加的参数
 * @param string $arrOption['after'] 字段值之后的填充数据
 * @return array
 * /
public function recursionFill($arrTemp,$arrOption)
{
    $arrRes = array();
        foreach($arrTemp as $k=>$v)
        {
            $strPar = $arrOption['before'].$v[$arrOption['key']];
            if(isset($arrOption['middle']))
            {
                $strPar .= $arrOption['middle'].$v[$arrOption['key']];
            }
            $strPar .= $arrOption['after'];
            $arrTemp[$k][$arrOption['key']] = $strPar;
            if(!empty($arrTemp[$k]['children']))
            {
                $arrTemp[$k]['children'] = recursionFill($arrTemp[$k]['children'],$arrOption);
            }
            $arrRes = $arrTemp;
        }
        return $arrRes;
    }
?>

实例

  • 调用前
Array
(
    [0] => Array
        (
            [id] => 1000100100
            [topic] => C
            [fatherid] => 0
            [direction] => right
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 1000101100
                            [topic] => 第一章
                            [fatherid] => 1000100100
                            [direction] => right
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 1000101101
                                            [topic] => 第一节
                                            [fatherid] => 1000101100
                                            [direction] => right
                                            [children] => Array
                                                (
                                                )

                                        )

                                )

                        )

                )

        )

    [1] => Array
        (
            [id] => 1002100100
            [topic] => C++
            [fatherid] => 0
            [direction] => right
            [children] => Array
                (
                )

        )

    [2] => Array
        (
            [id] => 1003100100
            [topic] => Objective-C
            [fatherid] => 0
            [direction] => right
            [children] => Array
                (
                )

        )

    [3] => Array
        (
            [id] => 1004100100
            [topic] => C#
            [fatherid] => 0
            [direction] => right
            [children] => Array
                (
                )

        )

    [4] => Array
        (
            [id] => 1005100100
            [topic] => PHP
            [fatherid] => 0
            [direction] => right
            [children] => Array
                (
                )

        )

)
  • 调用
$arrResult = $this -> recursionFill($arrTree,
                    ['key'=>'id',
                    'before'=>'/knowledge/index?kid=',
                    'middle'=>'#',
                    'after'=>'|_blank']
                );
  • 调用后
Array
(
    [0] => Array
        (
            [id] => /knowledge/index?kid=1000100100#1000100100|_blank
            [topic] => C
            [fatherid] => 0
            [direction] => right
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => /knowledge/index?kid=1000101100#1000101100|_blank
                            [topic] => 第一章
                            [fatherid] => 1000100100
                            [direction] => right
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => /knowledge/index?kid=1000101101#1000101101|_blank
                                            [topic] => 第一节
                                            [fatherid] => 1000101100
                                            [direction] => right
                                            [children] => Array
                                                (
                                                )

                                        )

                                )

                        )

                )

        )

    [1] => Array
        (
            [id] => /knowledge/index?kid=1002100100#1002100100|_blank
            [topic] => C++
            [fatherid] => 0
            [direction] => right
            [children] => Array
                (
                )

        )

    [2] => Array
        (
            [id] => /knowledge/index?kid=1003100100#1003100100|_blank
            [topic] => Objective-C
            [fatherid] => 0
            [direction] => right
            [children] => Array
                (
                )

        )

    [3] => Array
        (
            [id] => /knowledge/index?kid=1004100100#1004100100|_blank
            [topic] => C#
            [fatherid] => 0
            [direction] => right
            [children] => Array
                (
                )

        )

    [4] => Array
        (
            [id] => /knowledge/index?kid=1005100100#1005100100|_blank
            [topic] => PHP
            [fatherid] => 0
            [direction] => right
            [children] => Array
                (
                )

        )

)

你可能感兴趣的:(PHP,函数,递归,插件)