首页 > 代码库 > 标记1236

标记1236

<?php/*$level=3;$arr[‘children‘]=array();$newarr=array();global $newarr;for ($i=1;$i<=$level;$i++){    array_push($newarr[‘children‘],$i) ;    array_push($newarr,$newarr[‘children‘]) ;}var_dump($newarr);*/$order_item = Array ( Array ( ‘order_id‘ => 1111 ,‘item_id‘ => 1, ‘pro_id‘ => 1, ‘pro_name‘ => 1111 ,‘buy_num‘ => 5),Array ( ‘order_id‘ => 1111 ,‘item_id‘ => 2 ,‘pro_id‘ => 2 ,‘pro_name‘ => 2222, ‘buy_num‘ => 5 ),Array ( ‘order_id‘ => 2222 ,‘item_id‘ => 3 ,‘pro_id‘ => 1 ,‘pro_name‘ => 2222, ‘buy_num‘ => 1 ),Array ( ‘order_id‘ => 2222 ,‘item_id‘ => 4 ,‘pro_id‘ => 2 ,‘pro_name‘ => 2222, ‘buy_num‘ => 1 ));$dataArr = array(        array(        ‘dept_id‘=>1,        ‘dept_name‘=>‘一级菜单 ‘,        ‘_parentId‘=>‘‘        ),        array(                ‘dept_id‘=>2,                ‘dept_name‘=>‘1二级菜单1 ‘,                ‘_parentId‘=>‘1‘        ),        array(                ‘dept_id‘=>3,                ‘dept_name‘=>‘1二级菜单2 ‘,                ‘_parentId‘=>‘1‘        ),        array(                ‘dept_id‘=>4,                ‘dept_name‘=>‘一级菜单001 ‘,                ‘_parentId‘=>‘‘        ),        array(                ‘dept_id‘=>5,                ‘dept_name‘=>‘4二级菜单0012 ‘,                ‘_parentId‘=>‘4‘        ),        array(                ‘dept_id‘=>6,                ‘dept_name‘=>‘4二级菜单0012 ‘,                ‘_parentId‘=>‘5‘        ),        array(                ‘dept_id‘=>7,                ‘dept_name‘=>‘4二级菜单0012 ‘,                ‘_parentId‘=>‘6‘        ),    );//顶级节点function getParentArr(&$dataArr){        $parentArr = array();//顶级节点        foreach($dataArr as $key=>$arr)    {        if($arr[‘_parentId‘] ==‘‘ ){            array_push($parentArr,$arr);            unset($dataArr[$key]);        }    }    $dataArr = array_values($dataArr);//剩余子节点 重建索引    return $parentArr;}$parentArr = getParentArr($dataArr);//var_dump($dataArr);echo ‘顶级目录BE<br>‘;var_dump($parentArr);echo ‘顶级目录END<br>‘;//二级目录static $level=0;$secondArr=array();function getSecondArr(&$parentArr,&$dataArr){    global $secondArr;        foreach ($parentArr as $pkey=>$pval)    {        $pval[‘children‘]=array();                                 foreach ($dataArr as $key=>$val){          if($val[‘_parentId‘] == $pval[‘dept_id‘] ){               array_push($pval[‘children‘],$dataArr[$key]);                            unset($dataArr[$key]);          }                           }              array_push($secondArr,$pval);    }                //三级开始        if (count($dataArr)>0)    {        getSecondArr($parentArr[‘children‘],$dataArr);    }                var_dump($dataArr);    return     $secondArr;    }//二级$secondArr = getSecondArr($parentArr,$dataArr);var_dump($secondArr);foreach ($secondArr as $skey=>$sval){        }exit;global $dataArr;$resultArr=array();foreach($parentArr as $pkey=>$pVal){    $pVal[‘children‘]=array();    foreach($dataArr as $key => $arr){                if ($pVal[‘dept_id‘] == $arr[‘_parentId‘] ) {                        array_push($pVal[‘children‘],$dataArr[$key]);                        unset($dataArr[$key]);          }    }       /*foreach($pVal[‘children‘] as $Ckey=>$Cval)   {        $Cval[‘children‘]=array();        foreach ($dataArr as $lkey => $larr){                        if($Cval[‘dept_id‘] == $larr[‘_parentId‘]){                array_push($Cval[‘children‘],$dataArr[$lkey]);                unset($dataArr[$lkey]);                array_push($pVal[‘children‘],$Cval);            }         }     //var_dump($val);   }*/  // var_dump($pVal);     $pVal =  arrayChildren($pVal,$dataArr);  //$pVal =  recursionArr($pVal,$dataArr);   var_dump($pVal);}//$pVal =  arrayChildren($pVal,$dataArr);   echo ‘剩余的arr‘;    var_dump($dataArr);   echo ‘剩余的arr_end‘;      //递归调用    function recursionArr(&$arr,&$larrs)   {        arrayChildren($arr,$larrs);                if (count(&$larrs)>0)        {            recursionArr($arr,$larrs);        }   }         function arrayChildren(&$arr,&$larrs){    echo ‘arrayChildren‘;    if (is_array($arr[‘children‘])){              foreach($arr[‘children‘] as $key=>$val)        {            $val[‘children‘]=array();            foreach ($larrs as $lkey => $larr){                /*echo ‘<br>lkey_beg‘;                var_dump($larr);                echo ‘<br>lkey_end‘;                */                if($val[‘dept_id‘] == $larr[‘_parentId‘]){                    //global $larrs;                    array_push($val[‘children‘],$larrs[$lkey]);                    unset($larrs[$lkey]);                    array_push($arr,$val);                                    }            }        }                     }     //var_dump(‘count‘.count(&$larrs));            echo ‘<br>a<br>‘;        var_dump($larrs);            echo ‘b<br>‘;             return $arr;     }//分组问题exit;$rows = array(        array("id"=>1, "type"=>1, "value"=>"a"),        array("id"=>2, "type"=>1, "value"=>"b"),        array("id"=>3, "type"=>1, "value"=>"c"),        array("id"=>7, "type"=>3, "value"=>"c"),        array("id"=>8, "type"=>7, "value"=>"c"),        array("id"=>4, "type"=>2, "value"=>"d"),        array("id"=>5, "type"=>2, "value"=>"e"),        array("id"=>6, "type"=>2, "value"=>"f"),);$keys = array("id","type");function groupByKeys($rows,$keys,$ext=‘_‘) {    $res = array();    foreach($rows as $row) {        $tmp = $row;        foreach($keys as $k) {            //为了利用array_merge_recursive方法的特性,使用非数字的键            $tmp = array($k.$ext.$row[$k] => $tmp);        }        $res = array_merge_recursive($res,$tmp);    }    $replace_keys = array();    foreach($keys as $k) {        $replace_keys[] = $k.$ext;    }    $str = str_replace($replace_keys,‘‘,json_encode($res));    return json_decode($str,true);}var_dump(groupByKeys($rows,$keys));//var_dump($resultArr);exit;function generateTree($items){    $tree = array();    foreach($items as $item){        if(isset($items[$item[‘pid‘]])){            $items[$item[‘pid‘]][‘children‘][] = &$items[$item[‘id‘]];        }else{            $tree[] = &$items[$item[‘id‘]];        }    }    return $tree;}$items = array(        1 => array(‘id‘ => 1, ‘pid‘ => 0, ‘name‘ => ‘安徽省‘),        2 => array(‘id‘ => 2, ‘pid‘ => 0, ‘name‘ => ‘浙江省‘),        3 => array(‘id‘ => 3, ‘pid‘ => 1, ‘name‘ => ‘合肥市‘),        4 => array(‘id‘ => 4, ‘pid‘ => 3, ‘name‘ => ‘长丰县‘),        5 => array(‘id‘ => 5, ‘pid‘ => 1, ‘name‘ => ‘安庆市‘),);var_dump(generateTree($items));

 

标记1236