noganno
6/4/2016 - 3:27 AM

дерево каталогов http://yiiframework.ru/forum/viewtopic.php?t=14520

<?php 
function makeTree($rows=array(),$idName='id',$pidName='parent_id')
        {
            if(!isset($rows) or empty($rows) or !is_array($rows)) return false;

            $children = array(); // children of each ID
            $ids = array();
            foreach ($rows as $i=>$r)
            {
                $row =& $rows[$i];
                $id = $row[$idName];
                if ($id === null)
                {
                    continue;
                }
                $pid = $row[$pidName];
                if ($id == $pid)
                {
                    $pid = null;
                }
                $children[$pid][$id] =& $row;
                if (!isset($children[$id]))
                {
                    $children[$id] = array();
                }
                $row['hasChildren']=!empty($row['children']);
                $row['children'] =& $children[$id];
                $ids[$id] = true;
            }
            // Root elements are elements with non-found PIDs.
            $forest = array();
            foreach ($rows as $i=>$r)
            {
                $row =& $rows[$i];
                $id = $row[$idName];
                $pid = $row[$pidName];
                if ($pid == $id) $pid = null;
                if (!isset($ids[$pid]))
                {
                    $forest[$row[$idName]] =& $row;
                }
                //unset($row[$idName]);
                //unset($row[$pidName]);
            }
            return $forest;
        }