/** * reorders list with parent and child properly positioned * * @param array $rows * @param int $parentId * @return array */ static public function positionParents( $rows, $parentId = 0 ) { $parents = $rows; if ( $parents ) foreach ( $parents as $k => $v ) { if ( $v->parent != $parentId ) { unset( $parents[$k] ); } } $list = array(); $order = array(); if ( ! empty( $parents ) ) foreach ( $parents as $parent ) { $list[] = $parent; $order[] = $parent->id; $children = $rows; if ( $children ) foreach ( $children as $k => $v ) { if ( $v->parent != $parent->id ) { unset( $children[$k] ); } } if ( ! empty( $children ) ) foreach ( $children as $child ) { $list[] = $child; $order[] = $child->id; $sub_children = cbgjData::positionParents( $rows, $child->id ); if ( ! empty( $sub_children ) ) foreach ( $sub_children as $sub_child ) { $list[] = $sub_child; $order[] = $sub_child->id; } } } return $list; }