Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 创建数组元素的分层列表_Php_Arrays - Fatal编程技术网

Php 创建数组元素的分层列表

Php 创建数组元素的分层列表,php,arrays,Php,Arrays,我有一系列的产品。每个产品都包含按层次顺序列出的类别和子类别: Array ( [product_id_1] => Array ( [0] => Men [1] => Sunglasses [2] => Luxury [3] => Ray-ban ) [product_id_2] => Array ( [0] =

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

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 ) [...] ) 排列 ( [product\u id\u 1]=>阵列 ( [0]=>男性 [1] =>太阳镜 [2] =>豪华 [3] =>雷班 ) [product\u id\u 2]=>阵列 ( [0]=>女性 [1] =>镜头 [2] =>休闲 [3] =>古奇 ) [product_id_3]=>阵列 ( [0]=>男性 [1] =>太阳镜 [2] =>休闲 [3] =>普拉达 ) [...] ) 我想创建一个无序的分层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具有强大的数组功能:字符串索引数组可以帮助解决类似这样的问题

对于阵列转换步骤:

$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编写步骤(递归秘密酱=一个简单的递归函数,包括基本情况下的条件分支):


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

以下是一个简单的想法:

$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'
    )
);
我们的想法是根据父类别重新创建键,然后使用ksort()对它们进行排序:

函数树($array){
$newArray=array();
foreach($arr形式的数组){
foreach($arr作为$key=>$row){
如果($key>0){
$index=array();
对于($i=0;$i$行){
if(strcmp($row,$key)==0&&$i!=0)
echo'
    ; ++$i; $level=计数(分解(“',$key)); $padding=15*(-$level); 回声 “
  • “.$行。”
  • '; } 回声“
”;
您可以以静态方式变换数组,因为您描述的结构总是由四部分组成

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

你在创建数组或绘制数组时有问题吗?@Simo creating it:)你创建的函数,它显示了什么?@Simo it返回一个产品ID数组,其中包含按层次顺序排列的类别和子类别。我在文章的开头发布了该数组。你想用它显示层次菜单吗?所以你想显示放置它而不是创建它:)非常感谢!正是我所需要的。
$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'
    )
);
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;
}
$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>';
$hierarchy = array();
foreach($products as $product_id => $product) {
    list($gender, $category, $type, $brand) = $product;
    $hierarchy[$gender][$category][$type][$brand][] = $product_id;
}