タグ1236

24094 ワード

<?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));