Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/265.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

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_Multidimensional Array - Fatal编程技术网

Php 如何基于路径样式值创建数组树?

Php 如何基于路径样式值创建数组树?,php,arrays,multidimensional-array,Php,Arrays,Multidimensional Array,我对多维数据数组有一个问题。我有这样一个数据数组: [ [ "name" => "netSnmp", "oid" => "1.3.6.1.4.1.8072" "status" => "current" ], [ "name" => "netSnm

我对多维数据数组有一个问题。我有这样一个数据数组:

[
    [
        "name" => "netSnmp",
        "oid" => "1.3.6.1.4.1.8072"
        "status" => "current"
    ], [
        "name" => "netSnmpObjects",
        "oid" => "1.3.6.1.4.1.8072.1"
    ], [
        "name" => "netSnmpEnumerations",
        "oid" => "1.3.6.1.4.1.8072.3"
    ], [
        "name" => "netSnmpModuleIDs",
        "oid" => "1.3.6.1.4.1.8072.3.1"
    ], [
        "name" => "netSnmpAgentOIDs",
        "oid" => "1.3.6.1.4.1.8072.3.2"
    ], [
        "name" => "netSnmpDomains",
        "oid" => "1.3.6.1.4.1.8072.3.3"
    ], [
        "name" => "netSnmpNotificationPrefix",
        "oid" => "1.3.6.1.4.1.8072.4"
    ], [
        "name" => "netSnmpNotifications",
        "oid" => "1.3.6.1.4.1.8072.4.0"
    ], [
        "name" => "netSnmpNotificationObjects",
        "oid" => "1.3.6.1.4.1.8072.4.1"
    ]
]
我正在寻找一种基于上面数组中的
oid
值创建数组树的简单方法。那些
oid
值是点分隔的路径。部分越多,对应项在最终树中放置的越深

所需输出:

[
    "text" => "netSnmp",
    "oid" => "1.3.6.1.4.1.8072",
    "nodes" => [
        [
            "oid" => "1.3.6.1.4.1.8072.1",
            "text" => "netSnmpObjects"
        ], [
            "oid" => "1.3.6.1.4.1.8072.3",
            "text" => "netSnmpEnumerations",
            "nodes" => [
                [
                    "text" => "netSnmpModuleIDs",
                    "oid" => "1.3.6.1.4.1.8072.3.1"
                ], [
                    "text" => "netSnmpAgentOIDs",
                    "oid" => "1.3.6.1.4.1.8072.3.2"
                ], [
                    "text" => "netSnmpDomains",
                    "oid" => "1.3.6.1.4.1.8072.3.3"
                ]
            ]
        ], [
            "oid" => "1.3.6.1.4.1.8072.4",
            "text" => "netSnmpNotificationPrefix"
            "nodes" => [
                [
                    "text" => "netSnmpNotifications",
                    "oid" => "1.3.6.1.4.1.8072.4.0"
                ], [
                    "text" => "netSnmpNotificationObjects",
                    "oid" => "1.3.6.1.4.1.8072.4.1"
                ]
            ]
        ]
    ]
]

你知道怎么解决吗?

你可以通过
oid
在一个新的关联数组中为你的数据设置密钥:这样你就可以快速地查找某个父密钥

然后,只需获取任意给定键的父键(切掉最后一个点分隔的部分)并查看它在新关联数组中的位置,然后将其附加到其
节点
数组

下面是一个函数,用于:

function makeTree($arr) {
    $keyed = [];
    foreach ($arr as $item) $keyed[$item["oid"]] = $item;
    $root = null;
    
    foreach ($keyed as &$item) {
        $key = $item["oid"];
        $parent = substr($key, 0, strrpos($key, "."));
        if (isset($keyed[$parent])) {
            $keyed[$parent]["nodes"][] =& $item;
        } else {
            $root = $key;
        }
    }
    return $keyed[$root];
}
您可以在示例数据上这样运行它:

$arr = [
    [
        "name" => "netSnmp",
        "oid" => "1.3.6.1.4.1.8072",
        "status" => "current"
    ], [
        "name" => "netSnmpObjects",
        "oid" => "1.3.6.1.4.1.8072.1"
    ], [
        "name" => "netSnmpEnumerations",
        "oid" => "1.3.6.1.4.1.8072.3"
    ], [
        "name" => "netSnmpModuleIDs",
        "oid" => "1.3.6.1.4.1.8072.3.1"
    ], [
        "name" => "netSnmpAgentOIDs",
        "oid" => "1.3.6.1.4.1.8072.3.2"
    ], [
        "name" => "netSnmpDomains",
        "oid" => "1.3.6.1.4.1.8072.3.3"
    ], [
        "name" => "netSnmpNotificationPrefix",
        "oid" => "1.3.6.1.4.1.8072.4"
    ], [
        "name" => "netSnmpNotifications",
        "oid" => "1.3.6.1.4.1.8072.4.0"
    ], [
        "name" => "netSnmpNotificationObjects",
        "oid" => "1.3.6.1.4.1.8072.4.1"
    ]
];

$result = makeTree($arr);
print_r($result);

那么到底是什么问题呢?在这个例子中,对事物进行分组的标准是什么?谢谢@Mihail Minkov,我想根据oid值进行树状排列。请使用oid的树状层次规则更新您的问题,这样对人们理解问题会更有用。这个函数工作得很好,现在我可以根据我的需要开发它。非常感谢你的帮助。