Article From:https://segmentfault.com/q/1010000012143003

This site tree is the URL field in the table, and how to process the data into a rendering. I have dealt with the segmentation. I hope the responder will help to provide the following code, not the train of thought. Thinking I have a spelling of data, hard to spell.
Expected effect:

The 1 idea I tried was to get rid of the repeated values in the array, but after removing the duplicate values, how did I know who was on the next level? This scheme is pass dropped
The 2 idea I tried was to find the same value in the array and merge it into a new array.
Idea 2: the code is as follows

What is happening now: there are 1 records that are not shown and may be data coverage, but I have finished printing, and I didn’t find out at which level the problem was at the level. I don’t know how to deal with it. Or if you have a better way, I hope you can give me some advice.

Answer 0:

Well, everyone needs to test it

$urlArr = array(



$urlTree = arrangeUrlTree($urlArr); //Stitching data
echo “<pre>”;


  • Collate the URL array as the structure of the URL tree
  • @param $arr One-dimensional array
  • @return array
  • @author xg

function arrangeUrlTree($arr){

    return array();
$returnArr = array();
foreach($arr as $k => $v){
    $url = parse_url($v);//Analysis of URL//print_r ($url);$hostname = $url['scheme'].': //'.$url['host'];If (! Isset ($returnA)Rr[$hostname])) {$returnArr[$hostname] = array ();}$pathList = explode ('/', $url['pAth']);$pathList = array_filter ($pathList); / / to empty$pathList = array_merge ($pathList); / / rescheduled subscript$pathListCount = count ($pathList); / / length$tmpArr = & $returnArr[$hostname]; / / reference assignment to update the value of the final array.$treeLevel = 1; the depth of / / tree is used for traversalForeach ($pathList as $k2 => $v2) {If ($k2 = = ($pat)HListCount-1)) the last layer is a file, otherwise it is a folder path.$tmpArr[] = $v2;//$tmpArr['treeLevEl'] = $treeLevel; / / grade}else{If (! Isset ($tmpArr[$v2])) {$tmpArr[$v2] = array ();}//$tmpArr['treeLevel'] = $treeLevel;$tmpARR = & $tmpArr[$v2]; / / re assignment array for completing the completed directory circulate.}$treeLevel + = 1;}}Return $retUrnArr;


Leave a Reply

Your email address will not be published. Required fields are marked *