简体   繁体   English

创建和管理嵌套数组

[英]Create and manage nested array

I've tried for a while to figure out how to get this to work and after a lot of Google and not being able to find a problem similar to mine (maybe I'm looking for the wrong thing?) I decided to ask: 我已经尝试了一段时间,以弄清楚如何使它正常工作,并且经过大量Google的尝试,却找不到类似于我的问题(也许我在找错东西?),我决定问:

I've got incoming data that shows essentially a binary tree of IRC server UUIDs, that is the server's UUID and its parent's UUID. 我得到的传入数据实质上显示了IRC服务器UUID的二进制树,即服务器的UUID和其父服务器的UUID。 What I want to do is create an array that's a tree of this, the incoming data is sort of as follows: 我想做的是创建一个数组,它是一棵这样的树,传入的数据如下:

Parent => Child
none => 01D
01D => 01B
01B => 01F
01B => 8OS
01B => 01k
01K => 01M

And I want it to be in an array such as: 我希望它位于诸如以下的数组中:

$tree = array('01D' => array(
                       '01B' => array(
                                '01F' => array(),
                                '8OS' => array(),
                                '01k' => array('01M' => array()));

I don't get the data all at once, and it's not in a database, so essentially I need to be able to add branches at will. 我不能一次获得所有数据,并且不在数据库中,因此从本质上讲,我需要能够随意添加分支。

The primary reason I want to do it this way is in case one of the server disappears, say "01F" I know which children are its and can go through those. 我要这样做的主要原因是,如果其中一台服务器消失了,说“ 01F”,我知道它是哪个子代,并且可以通过那些子代。 So, how would I loop through the children in that regard? 那么,在这方面,我将如何遍历孩子们? Essentially in an IRC context, dealing with netsplits. 本质上是在IRC上下文中,处理netsplis。

I'm not even sure if this is the best way to do this, probably not. 我什至不确定这是否是最好的方法,也许不是。

It makes no sense for the incoming data to be in this format: 传入的数据采用以下格式是没有意义的:

Parent => Child
none => 01D
01D => 01B
01B => 01F
01B => 8OS
01B => 01k
01K => 01M

...because the keys are non-unique (ex. 01B ). ...因为键是非唯一的(例如01B )。 You should flip the array so it looks like this: 您应该翻转数组,使其看起来像这样:

$uuids = array(
    '01D' => NULL,
    '01B' => '01D',
    '01F' => '01B',
    '8OS' => '01B',
    '01K' => '01B',
    '01M' => '01K'
);

Finding the children of a failed node is a simple loop and in_array() call: 查找故障节点的子级是一个简单的循环和in_array()调用:

$failedNodes = array('01B'); // original failed node

foreach ($uuids as $child => $parent) {
    if (in_array($parent, $failedNodes)) {
        $failedNodes[] = $child;
    }
}

var_dump($failedNodes);

Output: 输出:

array(4) {
  [0]=>
  string(3) "01B"
  [1]=>
  string(3) "01F"
  [2]=>
  string(3) "8OS"
  [3]=>
  string(3) "01k"
}

Well i usually use references or objects in this case. 好吧,我通常在这种情况下使用引用或对象。 What i'll show you here is how to use references, a pattern is like a lot. 我在这里向您展示的是如何使用引用,一个模式很像。

//Initialize your server array
$servers = array();

//Then, each time you get a new server to add, add it to the server list if it doesn't exist
if(!isset($servers[$myname])){
    $servers[$myname] = array(
        'name' => 'data1',
        'prop1' => 'data2',
        'prop2' => 'data3',
        'prop3' => 'data4',
        'prop4' => 'data5',
        'childrens' => array(),
    );
}

//Then add your server to the children of the appropriate server
if(isset($servers[$myparent])){
    $servers[$myparent]['childrens'][] = &$servers[$myname];
}

This will create a list of all possible servers and will link each entry to a children of another. 这将创建所有可能服务器的列表,并将每个条目链接到另一个条目。 This way, if you remove a server, you don't lose the data inside of this children, you only lose the reference. 这样,如果删除服务器,则不会丢失该子级内部的数据,而只会丢失引用。

If you where to foreach the children, you'd get the data you referenced, var_dump, print_r??? 如果您要在哪里教孩子,您将获得引用的数据,var_dump,print_r ??? They all work fine... unset($servers['thatoneserver']) you still keep all you server data but lose that server and the links to their children. 它们都可以正常工作... unset($ servers ['thatoneserver'])您仍然保留所有服务器数据,但是丢失了该服务器及其子节点的链接。

Et voila... 等等...

I wouldn't rebuild the tree. 我不会重建那棵树。 Simply maintain the parent-child relationship. 只需保持亲子关系即可。 From your primary reason you only care about the children anyway. 从您的首要原因出发,您还是只关心孩子。

$nodes = array(
  '01D' => array('01B'),
  '01B' => array('01F', '8OS', '01k'),
  // ...
);

Then when a node fails, you can easily find the children ( $nodes[$failed] ) and traverse from there. 然后,当节点发生故障时,您可以轻松找到子$nodes[$failed]$nodes[$failed] )并从那里遍历。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM