在PHP中重新排列多维数组

3
我有一个数组,需要稍微重新排列一下。我正在为pyrocms构建语音服务器状态小部件。

使用静态表格我创建了这个! www.i.stack.imgur.com/4a3Am.png

我得到了一个返回的数组,看起来是这样的:

Array (
"Ventrilo" => Array (
    "name" => "The Evil Teddys",
    "phonetic" => "The Evil Teddys",
    "comment" => "",
    "auth" => "1",
    "maxclients" => "20",
    "voicecodec" => "0,GSM 6.10",
    "voiceformat" => "3,44 KHz, 16 bit",
    "uptime" => "25799604",
    "platform" => "Linux-i386",
    "version" => "3.0.3",
    "channelcount" => "7",
    "teams" => Array (
        "0" => Array (
                "cid" => "846",
                "pid" => "0",
                "prot" => "0",
                "name" => "<TET>",
                "comm" => ""
            ),
        "1" => Array (
                "cid" => "847",
                "pid" => "0",
                "prot" => "0",
                "name" => "#ISOLATIECEL",
                "comm" => ""
            ),
        "2" => Array (
                "cid" => "848",
                "pid" => "0",
                "prot" => "0",
                "name" => "Relax Take It Easy",
                "comm" => ""
            ),
        "3" => Array (
                "cid" => "849",
                "pid" => "846",
                "prot" => "0",
                "name" => "Foodfightkitchen",
                "comm" => ""
            ),
        "4" => Array (
                "cid" => "850",
                "pid" => "846",
                "prot" => "0",
                "name" => "WOW",
                "comm" => ""
            ),
        "5" => Array (
                "cid" => "851",
                "pid" => "849",
                "prot" => "0",
                "name" => "Bad Company",
                "comm" => ""
            ),
        "6" => Array(
                "cid" => "852",
                "pid" => "850",
                "prot" => "0",
                "name" => "The G Channel",
                "comm" => ""
            )
    ),
    "clientcount" => "3",
    "players" => Array (
        "0" => Array (
                "admin" => "1",
                "cid" => "846",
                "phan" => "0",
                "ping" => "18",
                "sec" => "345345",
                "name" => "Shorty*",
                "comm" => ""
            ),
        "1" => Array (
                "admin" => "1",
                "cid" => "851",
                "phan" => "0",
                "ping" => "20",
                "sec" => "11988",
                "name" => "Swifty",
                "comm" => ""
            ),
        "2" => Array (
                "admin" => "1",
                "cid" => "846",
                "phan" => "0",
                "ping" => "30",
                "sec" => "678674",
                "name" => "The1one12",
                "comm" => ""
            )
    ),
    "gq_online" => "0",
    "gq_address" => "172.0.0.1",
    "gq_port" => "3812",
    "gq_prot" => "ventrilo",
    "gq_type" => "ventrilo"
)

我希望我的最终结果是这样的:

cid = 频道ID

pid = 父级ID

Array (
"Ventrilo" => Array (
    "name" => "The Evil Teddys",
    "phonetic" => "The Evil Teddys",
    "comment" => "",
    "auth" => "1",
    "maxclients" => "20",
    "voicecodec" => "0,GSM 6.10",
    "voiceformat" => "3,44 KHz, 16 bit",
    "uptime" => "25799604",
    "platform" => "Linux-i386",
    "version" => "3.0.3",
    "channelcount" => "7",
    "teams" => Array (
        "0" => Array (
                "cid" => "846",
                "pid" => "0",
                "prot" => "0",
                "name" => "<TET>",
                "comm" => "",
                "players" => Array (
                    "0" => Array (
                            "admin" => "1",
                            "cid" => "846",
                            "phan" => "0",
                            "ping" => "18",
                            "sec" => "345345",
                            "name" => "Shorty*",
                            "comm" => "vet verwacht je niet"
                        ),
                    "1" => Array (
                            "admin" => "1",
                            "cid" => "846",
                            "phan" => "0",
                            "ping" => "30",
                            "sec" => "678674",
                            "name" => "The1one12",
                            "comm" => "grappig !"
                        )
                ),
                "teams" => Array(
                    "0" => Array (
                            "cid" => "849",
                            "pid" => "846",
                            "prot" => "0",
                            "name" => "Foodfightkitchen",
                            "comm" => "",
                            "players" => Array (),
                            "teams" => Array(
                                "0" => Array (
                                        "cid" => "851",
                                        "pid" => "849",
                                        "prot" => "0",
                                        "name" => "Bad Company",
                                        "comm" => "",
                                        "players" => Array (
                                                "0" => Array (
                                                        "admin" => "1",
                                                        "cid" => "851",
                                                        "phan" => "0",
                                                        "ping" => "20",
                                                        "sec" => "11988",
                                                        "name" => "Swifty",
                                                        "comm" => "nu nog Dynamisch"
                                                    )
                                        ),
                                        "teams" => Array(

                                        )
                                    )
                            )
                        ),
                    "1" => Array (
                            "cid" => "850",
                            "pid" => "846",
                            "prot" => "0",
                            "name" => "WOW",
                            "comm" => "",
                            "players" => Array (),
                            "teams" => Array(
                                "0" => Array(
                                        "cid" => "852",
                                        "pid" => "850",
                                        "prot" => "0",
                                        "name" => "The G Channel",
                                        "comm" => "",
                                        "players" => Array (),
                                        "teams" => Array(

                                        )
                                    )
                            )
                        )
                )
            ),
        "1" => Array (
                "cid" => "847",
                "pid" => "0",
                "prot" => "0",
                "name" => "#ISOLATIECEL",
                "players" => Array (),
                "teams" => Array(

                )
            ),
        "2" => Array (
                "cid" => "848",
                "pid" => "0",
                "prot" => "0",
                "name" => "Relax Take It Easy",
                "comm" => "",
                "players" => Array (),
                "teams" => Array(

                )
            )
    ),
    "clientcount" => "3",
    "gq_online" => "1",
    "gq_address" => "213.163.76.130",
    "gq_port" => "3812",
    "gq_prot" => "ventrilo",
    "gq_type" => "ventrilo"
)

我应该怎样做才能实现这个?我尝试了很多方法,但没有一个能够达到这个结果。

我目前拥有的是什么。

$teamArrayConverted['teams'] = convertTeamArray($serverstatus['teams']);
    unset($serverstatus['players'], $serverstatus['teams']);
$results = array_merge_recursive($serverstatus, $teamArrayConverted);

    function convertTeamArray ($array) {
    // First, convert the array so that the keys match the ids
    $reKeyed = array();
    foreach ($array as $item) {
        $reKeyed[(int) $item['cid']] = $item;
    }
    //print_r($reKeyed);    
    // Next, use references to associate children with parents
    foreach ($reKeyed as $id => $item) {
        if (isset($item['pid'], $reKeyed[(int) $item['pid']])) {
          $reKeyed[(int) $item['pid']]['teams'][] =& $reKeyed[$id];
        }
    }
    //print_r($reKeyed);
    // Finally, go through and remove children from the outer level
    foreach ($reKeyed as $id => $item) {
        if ($item['pid'] != '0') {
          //print_r($reKeyed[$id]);
          unset($reKeyed[$id]);
        }
    }
    return array_values($reKeyed);
}

2
你尝试过什么?展示你的代码。没有人会为你编写代码。 - hakre
2个回答

1

它的原理如下:

  1. 您通过其数字ID使每个节点可识别,并将其存储在哈希中,以便可以通过其ID访问。
  2. 您获取一个新哈希并插入所有必要的元素及其子元素。

有关代码的详细讨论在此处:根据父ID值将数组从单维转换为多维

// key the array by id
$keyed = array();
foreach($array as &$value)
{
    $keyed[$value['id']] = &$value;
}
unset($value);
$array = $keyed;
unset($keyed);

// tree it
$tree = array();
foreach($array as &$value)
{
    if ($parent = $value['parent_id'])
        $array[$parent]['children'][] = &$value;
    else
        $tree[] = &$value;
}
unset($value);
$array = $tree;
unset($tree);

var_dump($array); # your result

使用变量名称和键替换名称。


我创建了一个函数:return array_values($reKeyed); 这样它就可以按照正常顺序设置键。 - Jeroen van Heel
现在我需要获取玩家! - Jeroen van Heel
这是程序代码的工作示例:http://theevilteddys.nl/rev91/server - Jeroen van Heel

0
根据当前数组中的pid创建一个数组,您可以使用该数组根据id循环遍历其子元素。
$arr = array(...definition from your post....);
foreach($arr["teams"] as $team)
    $teamchildren[$team[pid]][]=$team;
print_r($teamchildren);

现在你可以循环遍历$teamchildren[0]数组,并递归调用一个函数来构建每个团队下的嵌套团队结构。
相同的概念也可以应用于球员,但在这种情况下,将父级更改为cid
(上面的代码未经测试)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接