需要创建一个带有分隔符的字符串数组的树状结构

Need to create a treelike struct of array of strings with separator

我需要转换一个字符串数组,这是一个用“/”分隔的列表。

基本上我需要这个:

[
    "label1/label12/label13/label14",
    "label1/label12/label15",
    "label1/label12/label16",
    "label7/label72/label73"
]

变成这样:

{
    "label1": {
        "label12": {
            "label13": "label14",
            "label15",
            "label16"
        }
    },
    "label7": {
        "label72": "label73"
    }
}

这有可能吗?

编辑:在 解决了我的问题之后,正如我所写的那样,我现在需要一种方法来使用像“+++ In work +++”这样的键来完成它

Edit2:第二个 效果更好。谢谢大家!

<cfset arrLabels = [
    "label1/label12/label13/label14",
    "label1/label12/label15",
    "label1/label12/label16",
    "label7/label72/label73"
]>




<cffunction name="labelConversion" returntype="struct">
    <cfargument name="arrLabels" type="array">
    <cfset var structLabels = {} />

    <cfloop from="1" to="#ArrayLen(arguments.arrLabels)#" index="i">
        <cfset StructGet("structLabels."&replace(arguments.arrLabels[i],'/','.', 'all'))>
    </cfloop>

    <cfreturn structLabels />
</cffunction>


<cfdump var="#labelConversion(arrLabels)#">
<cfabort>

此方法也适用于带有“+”或“!”等特殊字符的键。或“+++ 在工作中+++”

<Cfset labels =[
    "label1/label12/label+++13/+++ In work +++",
    "label1/label12/label15",
    "label1/label12$$@!/label16",
    "123/label72/label73"
]>


<cffunction name="convertLabels" returntype="struct">
    <cfargument name="k" type="array">
    <Cfset var local = {}>
    <Cfset local.response = {}>

    <Cfloop from=1 to="#arraylen(arguments.k)#" index="i">
        <Cfset local.splitted = arguments.k[i].Split("/")>
        <cfset local.refPath = local.response>
        <Cfloop from=1 to="#arraylen(local.splitted)#" index="local.y">
            <cfif not structKeyExists(local.refPath,local.splitted[local.y])>
                <Cfset local.refPath[splitted[local.y]] = {}>
            </cfif>
            <Cfset local.refPath = local.refPath[local.splitted[local.y]]>
        </cfloop>
    </cfloop>

    <cfreturn local.response>
</cffunction>
<cfdump var="#convertLabels(labels)#">