使用PHP显示邻接列表数组

时间:2014-09-23 14:39:51

标签: php arrays recursion adjacency-list

我有以下数组需要显示为导航

[items] => Array
        (
            [Electronics] => Array
                (
                    [level2] => Array
                        (
                            [Home Use] => Array
                                (
                                    [level3] => Array
                                        (
                                            [Laptops] => Array
                                                (
                                                    [level4] => Array
                                                        (
                                                            [0] => HP A very simple laptop
                                                            [1] => HP Tablet 
                                                            [2] => Acer Laptop 
                                                        )

                                                )

                                            [Desktops] => Array
                                                (
                                                    [level4] => Array
                                                        (
                                                            [0] => Acer Tablet 
                                                        )

                                                )

                                            [Tablets] => Array
                                                (
                                                    [level4] => Array
                                                        (
                                                            [0] =>  
                                                        )

                                                )

                                        )

                                )

                            [Business] => Array
                                (
                                    [level3] => Array
                                        (
                                            [Tablets] => Array
                                                (
                                                    [level4] => Array
                                                        (
                                                            [0] => HP A very simple laptop
                                                            [1] => HP Tablet 
                                                            [2] => Touchmate 
                                                        )

                                                )

                                            [Laptops] => Array
                                                (
                                                    [level4] => Array
                                                        (
                                                            [0] => Compaq 
                                                            [1] => IBM 
                                                        )

                                                )

                                            [Desktop] => Array
                                                (
                                                    [level4] => Array
                                                        (
                                                            [0] => Lenovo 
                                                        )

                                                )

                                        )

                                )

                        )

                )

            [Hotels] => Array
                (
                    [level2] => Array
                        (
                            [5 Stars] => Array
                                (
                                    [level3] => Array
                                        (
                                            [Daily Basis] => Array
                                                (
                                                    [level4] => Array
                                                        (
                                                            [0] => Hilton 
                                                        )

                                                )

                                            [Montly Basis] => Array
                                                (
                                                    [level4] => Array
                                                        (
                                                            [0] => Hilton 
                                                            [1] => Intercon 
                                                            [2] => Marina Hotel 
                                                            [3] => Galaxy Plaza Hotel 
                                                        )

                                                )

                                        )

                                )

                            [4 Stars] => Array
                                (
                                    [level3] => Array
                                        (
                                            [Daily Basis] => Array
                                                (
                                                    [level4] => Array
                                                        (
                                                            [0] => Address 
                                                        )

                                                )

                                            [Montly Basis] => Array
                                                (
                                                    [level4] => Array
                                                        (
                                                            [0] => Armada Al Barsha Hotel 
                                                            [1] => Everest International Hotel 
                                                            [2] => New Hotel 
                                                            [3] => Good Hotel 
                                                        )

                                                )

                                        )

                                )

                            [3 Stars] => Array
                                (
                                    [level3] => Array
                                        (
                                            [Daily Basis] => Array
                                                (
                                                    [level4] => Array
                                                        (
                                                            [0] => Royal Garden Hotel 
                                                        )

                                                )

                                            [Montly Basis] => Array
                                                (
                                                    [level4] => Array
                                                        (
                                                            [0] => Galaxy Plaza Hotel 
                                                        )

                                                )

                                        )

                                )

                        )

                )

            [Entertainment] => Array
                (
                    [level2] => Array
                        (
                            [] => Array
                                (
                                    [level3] => Array
                                        (
                                            [] => Array
                                                (
                                                    [level4] => Array
                                                        (
                                                            [0] =>  
                                                        )

                                                )

                                        )

                                )

                        )

                )

            [Services] => Array
                (
                    [level2] => Array
                        (
                            [] => Array
                                (
                                    [level3] => Array
                                        (
                                            [] => Array
                                                (
                                                    [level4] => Array
                                                        (
                                                            [0] =>  
                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

)

现在......我想在无序列表中显示它。我有以下功能。

public function display($sorted_array)
{   


    $markup = '';

    foreach ($sorted_array as $key => $value) 
    {   

        if($key === 'Electronics' || $key ==='Hotels' || $key === 'Services' || $key === 'Entertainment')
        {
            $markup .= '<h1><li>';
        }

        $markup .= (is_array($value)) ? '<b>'.$key.'</b></li></ul></h1>'.$this->display($value) : '<ul><li>'.$value. '</li></ul>';

    }

    return '<ul>'.$markup . '</ul>';

}

Electronics

Home Use

Laptops

    HP A very simple laptop
    HP Tablet 
    Acer Laptop 

台式机

    Acer Tablet 

片剂

商业

Tablets

    HP A very simple laptop
    HP Tablet 
    Touchmate 

笔记本

    Compaq 
    IBM 

桌面

    Lenovo 

酒店

5 Stars

Daily Basis

    Hilton 

所有类别和子类别都以正确的顺序显示。但是,我需要知道我应该在哪里添加一个id,以便我可以为子类别添加样式并适当地放置它们。

1 个答案:

答案 0 :(得分:0)

如果您使用id,那么您将多次使用id,因此请使用class而不是id。一种方法是保持代表水平的计数器。根据你的功能正常,你会得到这样的东西:

public function strip($string)
{
    $string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
    $string = preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
    $string = strtolower($string); //To lower case  
    return $string;
}

public function display($sorted_array, $level=0)
{   
    $level++;

    $markup = '';

    foreach ($sorted_array as $key => $value) 
    {   

        if($key === 'Electronics' || $key ==='Hotels' || $key === 'Services' || $key === 'Entertainment')
        {
            $markup .= '<h1><li id="'.$this->strip($key).'" class="level-'.$level.'">';
        }

        $markup .= (is_array($value)) ? '<b>'.$key.'</b></li></ul></h1>'.$this->display($value,$level) : '<ul><li id="'.$this->strip($key).'" class="level-'.$level.'">'.$value. '</li></ul>';

    }

    return '<ul>'.$markup . '</ul>';
}

现在你可以在你的css中使用关卡了。

.level-1 {

}
.level-2 {

}
...