传递数组的正确方法

问题描述 投票:0回答:1

[这使我感到困惑,因为我认为代码是正确的,但是没有点击,这是我从Yanick Rochon那里挑选的代码,当数组经过硬编码(在注释区域中)时可以使用,但是当我从所有pa_mymelp术语中将数组推到一起时,它的行为与预期不符,它使用整行作为一项,而不是每行使用多个项,并且也不创建子项,不知道为什么..谢谢!:] >

    <?php
$thearray = array();
$terms = get_terms("pa_mymelp");
foreach ( $terms as $term ) {
$categories =  $term->name;
array_push($thearray, $categories);
}

$categoryLines = $thearray;

/*$categoryLines = array(
    "Dodge>2002>Ram 3500>5.9 359cid L6 DIESEL OHV>Engine>Engine Oil Cooler",
    "Dodge>2001>Ram 2500>5.9 359cid L6 DIESEL OHV>Engine>Engine Oil Cooler"
);*/


function buildCategoryTree($categoryLines, $separator) {
    $catTree = array();
    foreach ($categoryLines as $catLine) {
       $path = explode($separator, $catLine);
       $node = & $catTree;
       foreach ($path as $cat) {
           $cat = trim($cat);
           if (!isset($node[$cat])) {
               $node[$cat] = array();
           }
           $node = & $node[$cat];
       }
    }
    return $catTree;
}

function displayCategoryTree($categoryTree, $indent = '') {
    foreach ($categoryTree as $node => $children) {
        echo $indent . $node . "\n";
        displayCategoryTree($children, $indent . '|- ');
    }
}

$categoryTree = buildCategoryTree($categoryLines, '>');

function displayHtmlCategoryTree($categoryTree, $id = null, $pathSeparator = '>', $parents = '') {
    if (empty($categoryTree)) return '';

    $str = '<ul' . (!empty($id) ? ' id="'.$id.'"' : '') . '>';
    foreach ($categoryTree as $node => $children) {
        $currentPath = $parents . (empty($parents) ? '' : $pathSeparator) . $node;
        $str .= '<li title="' . $currentPath . '">' . $node . 
                displayHtmlCategoryTree($children, null, $pathSeparator, $currentPath) . 
                '</li>';
    }
    $str .= '</ul>';
    return $str;
}

echo displayHtmlCategoryTree($categoryTree, "test", '>');
?>

这使我感到困惑,因为我认为代码是正确的,但是有些东西没有点击,这是我从Yanick Rochon那里挑选的代码,当数组经过硬编码时才有效(在注释中...) >

php wordpress woocommerce tree categories
1个回答
0
投票

原来是我的定界符,'>'某种程度上破坏了代码,所以我选择了'/',它现在可以正常工作了,这是最终的代码,在我傻乎乎地使用网址和滑出按钮之前:

$thearray = [];
$terms = get_terms("pa_mymelp");
foreach ( $terms as $term ) {
$categories =  $term->name;
array_push($thearray, $categories);
}

$categoryLines = $thearray; 

function buildCategoryTree($categoryLines, $separator) {
    $catTree = array();
    foreach ($categoryLines as $catLine) {
       $path = explode($separator, $catLine);
       $node = & $catTree;
       foreach ($path as $cat) {
           $cat = trim($cat);
           if (!isset($node[$cat])) {
               $node[$cat] = array();
           }
           $node = & $node[$cat];
       }
    }
    return $catTree;
}

function displayCategoryTree($categoryTree, $indent = '') {
    foreach ($categoryTree as $node => $children) {
        echo $indent . $node . "\n";
        displayCategoryTree($children, $indent . '|- ');
    }
}

$categoryTree = buildCategoryTree($categoryLines, '/');

function displayHtmlCategoryTree($categoryTree, $id = null, $pathSeparator = '>', $parents = '') {
    if (empty($categoryTree)) return '';

    $str = '<ul' . (!empty($id) ? ' id="'.$id.'"' : '') . '>';
    foreach ($categoryTree as $node => $children) {
        $currentPath = $parents . (empty($parents) ? '' : $pathSeparator) . $node;
        $str .= '<li title="' . $currentPath . '">' . $node . 
                displayHtmlCategoryTree($children, null, $pathSeparator, $currentPath) . 
                '</li>';
    }
    $str .= '</ul>';
    return $str;
}

echo displayHtmlCategoryTree($categoryTree, "test", '/');
© www.soinside.com 2019 - 2024. All rights reserved.