簡體   English   中英

使用表在數組之外構建層次結構-PHP

[英]Building Hierarchy out of array using tables - PHP

我在以下情況下遇到困難,我有一個包含類別的數組,因此類別可以具有子類別,而子類別可以無限地具有子類別。 現在,我要嘗試實現的目標是以下幾點,但我無法做到這一點。

我在以下結構中有一個數組$items

Array
(
    [0] => Array
        (
            [label] => Main Cat
            [id] => 29
            [parent_id] => 19
        )

    [1] => Array
        (
            [label] => Main Cat
            [id] => 17
            [parent_id] => 19
        )

    [2] => Array
        (
            [label] => Main Cat
            [id] => 20
            [parent_id] => 19
            [items] => Array
                (
                    [0] => Array
                        (
                            [label] => Child Level 1
                            [id] => 21
                            [parent_id] => 20
                        )

                    [1] => Array
                        (
                            [label] => Child Level 1
                            [id] => 22
                            [parent_id] => 20
                            [items] => Array
                                (
                                    [0] => Array
                                        (
                                            [label] => Child Level 2
                                            [id] => 27
                                            [parent_id] => 22
                                            [items] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [label] => Child Level 3
                                                            [id] => 28
                                                            [parent_id] => 27
                                                        )

                                                )

                                        )

                                )

                        )

                    [2] => Array
                        (
                            [label] => Child Level 1
                            [id] => 23
                            [parent_id] => 20
                        )

                    [3] => Array
                        (
                            [label] => Child Level 1
                            [id] => 24
                            [parent_id] => 20
                            [items] => Array
                                (
                                    [0] => Array
                                        (
                                            [label] => Child Level 2
                                            [id] => 25
                                            [parent_id] => 24
                                        )

                                    [1] => Array
                                        (
                                            [label] => Child Level 2
                                            [id] => 26
                                            [parent_id] => 24
                                        )

                                )

                        )

                )

        )

)

現在,我希望將以下數組顯示在一個表中,其中每個子級別都縮進,並且如果應該將其從子級別3升級到子級別2,則顯示應該再次反轉。

<table border="2" width="100%">
    <tr>
        <td>Main Cat</td>
    </tr>
    <tr>
        <td>Main Cat</td>
    </tr>
    <tr>
        <td>Main Cat</td>
    </tr>
    <tr>
        <td style="margin-left:20px;">Child Level 1</td>
    </tr>
    <tr>
        <td style="margin-left:20px;">Child Level 1</td>
    </tr>
    <tr>
        <td style="margin-left:40px;">Child Level 2</td>
    </tr>
    <tr>
        <td style="margin-left:60px;">Child Level 3</td>
    </tr>
    <tr>
        <td style="margin-left:20px;">Child Level 1</td>
    </tr>
    <tr>
        <td style="margin-left:20px;">Child Level 1</td>
    </tr>
    <tr>
        <td style="margin-left:40px;">Child Level 2</td>
    </tr>
    <tr>
        <td style="margin-left:40px;">Child Level 2</td>
    </tr>
</table>

到目前為止,我的PHP代碼看起來像這樣,

public function renderCategoriesRecursive($items)
        {
                foreach($items as $item)
                {

                        $itemCount = count($item['items']);



                        echo CHtml::openTag('tr');
                            echo CHtml::openTag('td',array('class'=>$class));
                                echo $item['label'];
                            echo CHtml::closeTag('tr');
                        echo CHtml::closeTag('tr');


                        if(isset($item['items']) && $itemCount)
                        {

                                $this->renderCategoriesRecursive($item['items']);

                        }
                }
}

變量$ items包含上面的數組

我注意到您自己的解決方案引用了變量$ class,因此我也將其合並。 您確實不需要使用CHtml(除非您這樣做,但我從未見過這種情況)

function renderItems( $array, $class, $indent = 0  ) {

    foreach( $items as $item) {
        echo '<tr><td class="'.$class.'">'.$item['label'].'</td></tr>';

        if( isset( $item['items'] && count( $item['items'] )
            renderItems($item['items'],$class,$indent+1);
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM