简体   繁体   中英

Array reference from recursive PHP array search

I am new to PHP and not sure how referencing works in PHP as it is defiantly different from C++ and Java.

Basically I have a loop that looks for a 'tag' recursively. I want the location of that array element and then add I am pushing another array to it as a 'tag' within a 'tag' as in my case I have tags, sub-tags, sub-sub-tags ...

Here is the code:

private function organize_tags(&$tags = NULL)
{
if ($tags === NULL)
{
    return;
}


$organized_tags = array();

$highest_tag_level = -1;
foreach ($tags as $tag)
{
    if($tag['level'] > $highest_tag_level)
    {
        $highest_tag_level = $tag['level'];
    }
}

$current_tag_level = 0;
foreach ($tags as $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 as $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 as $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 someone could help tell me how I get this insertion I would be sooooooo happy.

So I created an inefficient method of getting back the results and inserting them. I used this bit of code to insert into array from here

Anyway here is the code:

    if ($tags === NULL)
    {
        return;
    }

    $organized_tags = array();

    $highest_tag_level = -1;
    foreach ($tags as $tag)
    {
        if ($tag['level'] > $highest_tag_level)
        {
            $highest_tag_level = $tag['level'];
        }
    }

    $current_tag_level = 0;
    foreach ($tags as $tag)
    {
        if ($tag['level'] == $current_tag_level)
        {
            array_push($organized_tags, $tag);
        }
    }
    $current_tag_level++;

    foreach ($tags as $tag)
    {
        foreach ($organized_tags as $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 someone finds this useful :D

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM