array reference from recusive php array search -
i new php , not sure how referencing works in php defiantly different c++ , java.
basically have loop looks 'tag' recursivly. want location of array element , add pushing array 'tag' within 'tag' in case have tags, sub-tags, sub-sub-tags ...
here code:
private function organize_tags(&$tags = null) { if ($tags === null) { return; } $organized_tags = array(); $highest_tag_level = -1; foreach ($tags $tag) { if($tag['level'] > $highest_tag_level) { $highest_tag_level = $tag['level']; } } $current_tag_level = 0; foreach ($tags $key => $tag) { if($tag['level'] == $current_tag_level) { array_push($organized_tags,$tag); unset($tags[$key]); } } $current_tag_level++; while ($current_tag_level <= $highest_tag_level) { foreach ($tags $tag) { if($tag['level'] == $current_tag_level) { $tag_to_find = $tag['upper_tag']; $parent_tag = $this->find_tag($organized_tags, $tag_to_find); if($parent_tag != null) { if(!is_array($parent_tag['tag'])) { $parent_tag['tag'] = array(); } array_push($parent_tag['tag'], $tag); } } } $current_tag_level++; } $tags = $organized_tags; } private function find_tag(&$tags, &$desired_tag_id) { $ret = null; foreach($tags $tag) { if($tag['id'] == $desired_tag_id) { return $tag; } else if (count($tag['tag']) > 0) { $ret = $this->find_tag($tag, $desired_tag_id); if($ret !== null) { return $ret; } } } return $ret; }
if tell me how insertion sooooooo happy.
so created inefficient method of getting results , inserting them. used bit of code insert array here
anyway here code:
if ($tags === null) { return; } $organized_tags = array(); $highest_tag_level = -1; foreach ($tags $tag) { if ($tag['level'] > $highest_tag_level) { $highest_tag_level = $tag['level']; } } $current_tag_level = 0; foreach ($tags $tag) { if ($tag['level'] == $current_tag_level) { array_push($organized_tags, $tag); } } $current_tag_level++; foreach ($tags $tag) { foreach ($organized_tags $o_tag) { if ($tag['upper_tag'] == $o_tag['id']) { $this->array_insert($organized_tags,$tag,(array_search($o_tag,$organized_tags)+1)); } } } return $organized_tags;
hope finds useful :d
Comments
Post a Comment