PHP : 获取 Select 嵌套选项组和字符串动态选项

PHP : Get Select with NESTED Optgroup and options dynamically by Strings

从字符串数组开始,如:

"mobili;tavolini_lato_divano",
"mobili;tavoli_da_pranzo",
"illuminazione;plafoniere",
"illuminazione;lampade_decorative",
"sculture;sculture_in_resina",
"sculture;sculture_in_resina;serie_limitata_tatoo",
"sculture;sculture_in_resina;sculture_resina_grandi",

需要创建一个 select 具有嵌套的 optgroup 和选项,基于“;”拆分,如果有 2 个元素,第一个(父)成为 optgroup,第二个(子)成为一个选项。

如果有 3 个元素(具有相同的第一个元素),第二个元素将成为一个选项组(在第一个选项组内 - 嵌套 - )和一个选项(3d 元素)。

如果有 4 个元素,以此类推。

实际上我做了这个例子,但只有当有 2 个计数时才有效。

带有输入和输出的完整示例:

https://wtools.io/php-sandbox/b63q

Select 执行并 HTML 作为输出以查看结果。
谢谢

首先创建元素数组的有序(嵌套)数组(我代码中的第 76-89 行),然后必须使用递归函数(我代码中的第 91-104 行) 创建一串选项和 optgroup。

最后,打印递归函数的输出(我代码中的第 107 行)。

<?php

/*
Row 1 : mobiletti_puzzle_3d;mobili
Output : "optgroup" -> mobiletti_puzzle_3d
            "option" -> mobili

Row 2 : sculture;sculture_in_resina
Output : "optgroup" -> sculture
            "option" -> sculture_in_resina

Row 3 : sculture;sculture_in_resina;sculture_resina_grandi
Output : "optgroup" -> sculture
            "optgroup" -> sculture_in_resina
                "option" -> sculture_resina_grandi

If they have the same parent must be added as Option of Row 2 

Row 4 : sculture;sculture_in_metallo
Output : "optgroup" -> sculture
            "option" -> sculture_in_metallo


Final Output ALL EXAMPLE :
"optgroup" -> mobiletti_puzzle_3d
    "option" -> mobili
"optgroup" -> sculture
    "option" -> sculture_in_resina
    "option" -> sculture_in_metallo
    "optgroup" -> sculture_in_resina
        "option" -> sculture_resina_grandi

Split by " ; " the count-1 , always will be an optgroup and this will generate a Nested Optgroup 

*/
$Categories = [
    "mobiletti_puzzle_3d;mobili",
    "mobili;sedie_da_pranzo",
    "mobili;sgabelli",
    "mobili;tavolini_da_salotto",
    "mobili;tavolini_lato_divano",
    "mobili;tavoli_da_pranzo",
    "sculture;sculture_decorate_in_vetro",
    "sculture;sculture_in_bronzo",
    "sculture;sculture_in_metallo",
    "sculture;sculture_in_resina;sculture_resina_grandi",
    "sculture;sculture_in_resina;sculture_resina_medie",
    "sculture;sculture_in_resina;sculture_resina_piccole",
    "sculture;sculture_in_resina;serie_limitata_tatoo",
    "illuminazione;plafoniere",
    "illuminazione;lampade_decorative",
    "illuminazione;lampade_da_terra",
    "illuminazione;lampade_da_tavolo",
    "illuminazione;lampade_da_comodino",
    "illuminazione;lampadari",
    "divani_e_poltrone;mobili",
    "decorazione_murale;stampe_su_telaio_estetico",
    "decorazione_murale;specchi_design_moderno",
    "decorazione_murale;quadri_scultura_in_metallo",
    "decorazione_murale;puzzle_3d_in_legno",
    "decorazione_murale;orologi_parete",
    "decorazione_murale;dipinti_su_telaio_estetico",
    "decorazione_murale;dipinti_su_plexiglas",
    "consolle;mobili",
    "colonne;mobili",
    "collage_3d;decorazione_murale",
    "appliques;illuminazione",
    "altri_oggetti;vasi",
    "altri_oggetti;posacenere",
    "altri_oggetti;portagioie",
    "altri_oggetti;portacandele",
    "altri_oggetti;oggetti_decorativi",
    "altri_oggetti;centrotavola"
];

$select = [];
foreach ($Categories as $row) {
    $pointer = &$select;
    $row = explode(';', $row);
    $ogCount = count($row) - 1;
    for ($i = 0; $i < $ogCount; $i++) {
        $row[$i] = $row[$i];
        if (empty($pointer[$row[$i]]))
            $pointer[$row[$i]] = [];
        $pointer = &$pointer[$row[$i]];
        if (($i + 1) >= $ogCount)
            $pointer[] = $row[$i + 1];
    }
}

function recursiveSelect($_options, $_space = '')
{
    if (!is_array($_options))
        return  '<option value="' . $_options . '">' . $_options . '</option>' . "\n";
    else {
        $tmp = '';
        foreach ($_options as $key => $option)
            if (!is_array($option)) {
                $tmp .= recursiveSelect($option);
            } else
                $tmp .= '<optgroup label="' . $_space . $key . '">' . recursiveSelect($option, $_space . str_repeat('&nbsp;', 4)) . '</optgroup>' . "\n";
        return $tmp;
    }
}
?>
<select>
    <?php echo (recursiveSelect($select)); ?>
</select>

此代码可以处理数组中所有深度的嵌套元素。

为了更好地显示 select optgroups,我将 space ($_space) 参数添加到递归函数(具有 4 spaces 在第 101 行)。