创建数组元素的分层列表

时间:2015-02-09 20:16:17

标签: php arrays

我有一系列产品。每个产品都包含按层次顺序列出的类别和子类别:

Array
(
    [product_id_1] => Array
        (
            [0] => Men
            [1] => Sunglasses
            [2] => Luxury
            [3] => Ray-ban
        )

    [product_id_2] => Array
        (
            [0] => Women
            [1] => Lenses
            [2] => Casual
            [3] => Gucci
        )
    [product_id_3] => Array
        (
            [0] => Men
            [1] => Sunglasses
            [2] => Casual
            [3] => Prada
        )
[...]
)

我想创建一个无序的分层HTML菜单,如下所示:

-Men
--Sunglasses
---Luxury
----Ray-ban
---Casual
----Prada
-Women
--Lenses
---Casual
----Gucci

该功能应删除重复的类别和子类别。此脚本返回我在顶部发布的产品数组:

<?php
    function displayNestedMenu( $posts, $taxonomies ) {
        foreach ( $posts as $post ) {
            foreach ( $taxonomies as $key => $taxonomy ) {
                $push = wp_get_object_terms( $post->ID, $taxonomy );
                if ( !empty( $push ) ) {
                        $list[$post->ID][] = $push[0]->name;
                }
            }
        }
        return $list;
    }
    print_r( displayNestedMenu( $posts, $taxonomies ) );
?>

<?php function displayNestedMenu( $posts, $taxonomies ) { foreach ( $posts as $post ) { foreach ( $taxonomies as $key => $taxonomy ) { $push = wp_get_object_terms( $post->ID, $taxonomy ); if ( !empty( $push ) ) { $list[$post->ID][] = $push[0]->name; } } } return $list; } print_r( displayNestedMenu( $posts, $taxonomies ) ); ?>

我想解决方案应该调用函数内部的函数但是在尝试了几个方法后我还没有成功。任何建议都表示赞赏!

3 个答案:

答案 0 :(得分:1)

PHP具有强大的数组功能:字符串索引数组可以帮助提供像这样的问题的解决方案。

对于阵列转换步骤:

$hrchy=array();
foreach($products AS $product){//$products is as per your first array, at start…
    hrchy_ins($hrchy,$product);
}
function hrchy_ins(array &$hierarchy,array $product){//$hierarchy should be passed by reference…
    if(\count($product)>0){//Condition necessary to implement base case, avoiding infinite recursion
        if(!isset($hierarchy[$product[0]])){$hierarchy[$product[0]]=array();}//Conditional execution ignores duplicates…
        if(\count($product)>1){hrchy_ins($hierarchy[$product[0]],\array_slice($product,1));}//Condition may not be strictly necessary (see condition above!)
}   }

我们现在可以使用递归方法进行进一步的HTML编写步骤(递归秘密酱=一个简单的递归函数,包括基本情况的分支条件):

function prod_list(array $hierarchy){
    if(\count($hierarchy)===0){
        return '';
    }else{
        $list='';
        $list.='<ul>';
        foreach($hierarchy AS $cat => $children){
            $list.='<li>'.$cat;
            $list.=prod_list($children);//Recursive step…
            $list.='</li>';
        }
        $list.='<ul>';
        return $list;
    }
}

最后,在定义函数之后,我们调用它:

echo(prod_list($hrchy));

免责声明:我尚未测试此代码。

答案 1 :(得分:1)

这是一个简单的想法:

$array = array(
    'product_id_1' => array(
        'Men',
        'Sunglasses',
        'Luxury',
        'Ray-ban'
    ),
    'product_id_2' => array(
        'Women',
        'Lenses',
        'Casual',
        'Gucci',
    ),
    'product_id_3' => array(
        'Men',
        'Sunglasses',
        'Casual',
        'Prada'
    )
);

我们的想法是根据父类别重新创建密钥,之后我们使用kso​​rt()对它们进行排序:

function tree($array){
    $newArray = array();
    foreach ($array as $arr) {
        foreach ($arr as $key => $row) {

            if ($key > 0) {
                $index = array();
                for ($i = 0; $i <= $key; $i++)
                    $index[] = $arr[$i];

                $index = implode('_', $index);
            } else
                $index = $row;
            $newArray[$index] = $row;
        }
    }

    ksort($newArray);

    return $newArray;
}

然后显示HTML:

$products = tree($array);
$i = 0;
    echo '<ul style="list-style-type:none">';
    foreach ($products as $key => $row) {

        if(strcmp($row, $key) == 0 && $i != 0)
            echo '</ul><br><ul style="list-style-type:none">';

        ++$i;
        $level = count(explode('_', $key));
        $padding = 15 * (--$level);

        echo
            '<li style="padding-left:' . $padding . 'px">
                <span style="border-left:1px dashed black;border-bottom:1px dashed black;">&nbsp;' . $row . '</span>
            </li>';
    }
    echo '</ul>';

答案 2 :(得分:0)

您可以以静态方式转换数组,因为您描述的结构总是有四个部分;

$hierarchy = array();
foreach($products as $product_id => $product) {
    list($gender, $category, $type, $brand) = $product;
    $hierarchy[$gender][$category][$type][$brand][] = $product_id;
}