<?php $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' ), ); //$_parentId='1'; $_parentIdArr = array(); $deptidArr = array(); foreach ($dataArr as $arr){ array_push($_parentIdArr,$arr['_parentId']); } foreach ($dataArr as $arr){ array_push($deptidArr,$arr['dept_id']); } $_parentIdArr = array_unique($_parentIdArr); var_dump($_parentIdArr); var_dump($deptidArr); $parentArr = array(); foreach($dataArr as $key=>$arr) { if($arr['_parentId'] =='' ){ array_push($parentArr,$arr); unset($dataArr[$key]); } } $dataArr = array_values($dataArr);//重建索引 var_dump($dataArr); echo '顶级目录BE<br>'; var_dump($parentArr); echo '顶级目录END<br>'; $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]); //var_dump($dataArr); unset($dataArr[$key]); } //array_push($pVal,$pVal['children']); } //array_push($resultArr,$pVal); var_dump($pVal); foreach($pVal as $key=>$val) { //var_dump($val); } } //var_dump($resultArr); exit; foreach($resultArr as $Rkey=>$Rval ) { echo 'last234243<br>'; //var_dump($dataArr); var_dump($Rval['children']); echo 'lastedc231230000000000<br>'; $Rval['children']=array(); foreach($dataArr as $key => $arr){ if($Rval[$Rkey]['dept_id']==$arr['_parentId']) { //var_dump($dataArr[$key]); array_push($Rval['children'],$dataArr[$key]); unset($dataArr[$key]); } } } var_dump($resultArr); if (count($dataArr)>0) { echo '112121ok'; } echo 'last'; //var_dump($newArr); //var_dump($dataArr); $items = array(); foreach($order_item as $item) { $order_id = $item['order_id']; unset($item['order_id']); if(!isset($items[$order_id])) { $items[$order_id] = array('order_id'=>$order_id, 'items'=>array()); } $items[$order_id]['items'][] = $item; } // 下面这一步可不要 sort($items); //var_dump($items);