萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> php編程 >> php數組轉換成樹的幾個例子

php數組轉換成樹的幾個例子

   Php代碼

 代碼如下    
* $sourceArr 原來的數組 
* $key 主鍵 
* $parentKey 與主鍵關聯的父主鍵 
* $childrenKey 生成的孩子的鍵名 

*/  
  
function arrayToTree($sourceArr, $key, $parentKey, $childrenKey)  
{  
    $tempSrcArr = array();  
    foreach ($sourceArr as  $v)  
    {  
        $tempSrcArr[$v[$key]] = $v;  
    }  
    $i = 0;  
    $count = count($sourceArr);  
    for($i = ($count - 1); $i >=0; $i--)  
    {  
        if (isset($tempSrcArr[$sourceArr[$i][$parentKey]]))  
        {  
           $tArr = array_pop($tempSrcArr);  
           $tempSrcArr[$tArr[$parentKey]][$childrenKey] = (isset($tempSrcArr[$tArr[$parentKey]][$childrenKey]) && is_array($tempSrcArr[$tArr[$parentKey]][$childrenKey])) ? $tempSrcArr[$tArr[$parentKey]][$childrenKey] : array();  
           array_push ($tempSrcArr[$tArr[$parentKey]][$childrenKey], $tArr);  
        }  
    }  
    return $tempSrcArr;  
}  
 

  Php代碼

  * 將數組轉換成樹

  * 例子:將 array(

  array('id'=>1,'parentId' => 0,'name'=> 'name1')

  ,array('id'=>2,'parentId' => 0,'name'=> 'name2')

  ,array('id'=>4,'parentId' => 1,'name'=> 'name1_4')

  ,array('id'=>15,'parentId' => 1,'name'=> 'name1_5')

  );轉換成

  * Array(

  [1] => Array([id] => 1

  [parentId] => 0

  [name] => name1

  [children] => Array(

  [0] => Array([id] => 15,[parentId] => 1,[name] => name1_5)

  [1] => Array([id] => 4,[parentId] => 1,[name] => name1_4)

  )

  )

  [2] => Array([id] => 2,[parentId] => 0,[name] => name2)

  )

  * @param array $sourceArr 要轉換的數組

  * @param string $key 數組中確認父子的key,例子中為“id”

  * @param string $parentKey 數組中父key,例子中為“parentId”

  * @param type $childrenKey 要在樹節點上索引子節點的key,例子中為“children”

  * @return array 返回生成的樹

  */

 代碼如下   function arrayToTree($sourceArr, $key, $parentKey, $childrenKey)  
{  
    $tempSrcArr = array();  
  
    $allRoot = TRUE;  
    foreach ($sourceArr as  $v)  
    {  
        $isLeaf = TRUE;  
        foreach ($sourceArr as $cv )  
        {  
            if (($v[$key]) != $cv[$key])  
            {  
                if ($v[$key] == $cv[$parentKey])  
                {  
                    $isLeaf = FALSE;  
                }  
                if ($v[$parentKey] == $cv[$key])  
                {  
                    $allRoot = FALSE;  
                }  
            }  
        }  
        if ($isLeaf)  
        {  
            $leafArr[$v[$key]] = $v;  
        }  
        $tempSrcArr[$v[$key]] = $v;  
    }  
    if ($allRoot)  
    {  
        return $tempSrcArr;  
    }  
    else  
    {  
        unset($v, $cv, $sourceArr, $isLeaf);  
        foreach ($leafArr as  $v)  
        {  
            if (isset($tempSrcArr[$v[$parentKey]]))  
            {  
                $tempSrcArr[$v[$parentKey]][$childrenKey] = (isset($tempSrcArr[$v[$parentKey]][$childrenKey]) && is_array($tempSrcArr[$v[$parentKey]][$childrenKey])) ? $tempSrcArr[$v[$parentKey]][$childrenKey] : array();  
                array_push ($tempSrcArr[$v[$parentKey]][$childrenKey], $v);  
                unset($tempSrcArr[$v[$key]]);  
         &nb
copyright © 萬盛學電腦網 all rights reserved