function get_children_ids($arr, $pid = 0) { $ret = array(); foreach ($arr as $item) { if ($item['parent_id'] == $pid) { $ret[] = $item['id']; $children_ids = get_children_ids($arr, $item['id']); $ret = array_merge($ret, $children_ids); } } return $ret; }
/** * Gets array of all childen of a branch * @param int $id Parent id to get children for * @param string $table Table to select * @param bool $recursion Used in recursion, do not use in function call * @return array Array of children IDs */ function get_children_ids($id, $table = 'geo', $recursion = false) { if ($recursion) { $return = false; } else { $return = $this->API->CACHE->get('trees', "{$table}-childs-{$id}"); } if ($return === false) { $return = array(); $children = $this->API->DB->query_return("SELECT id FROM {$table} WHERE parent_id={$id}"); //var_dump($children); if (!$children) { if (!$recursion) { $this->API->CACHE->set('trees', "{$table}-childs-{$id}", $return); } return array($id); } foreach ($children as $child) { //var_dump(get_children_ids($child['id'], $table,true)); $return = array_merge($return, get_children_ids($child['id'], $table, true)); } // remove parent-childred foreach ($return as $k => $r) { $c = $this->API->DB->get_row_count($table, " WHERE parent_id={$r}"); if ($c) { unset($return[$k]); } } $this->API->CACHE->set('trees', "{$table}-childs-{$id}", $return); return $return; } else { return $return; } }