3

少し再配置する必要がある配列があります。Pyrocm 用のボイス サーバー ステータス ウィジェットを作成しています。

これを作成した静的テーブルです。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);
}
4

2 に答える 2

1

原則として次のように機能します。

  1. 各ノードを数値 ID で識別できるようにし、ID でアクセスできるようにハッシュに格納します。
  2. 新しいハッシュを取得し、すべての要素を挿入します。必要に応じて彼らの子供たち。

コードの詳細な説明は次のとおりです。親 ID 値に基づいて配列を 1 次元から多次元に変換する:

// 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

名前を、変数名とキーとして持っているものに置き換えます。

于 2012-05-10T09:13:17.383 に答える
0

現在の配列から pid に基づいて配列を作成します。これを使用して、id に基づいて子をループできます。

$arr = array(...definition from your post....);
foreach($arr["teams"] as $team)
    $teamchildren[$team[pid]][]=$team;
print_r($teamchildren);

$teamchildren[0] 配列をループし、関数を再帰的に呼び出して、各チームの下にネストされたチーム構造を構築できるようになりました

同じ概念をプレーヤーに適用できますが、この場合は親をcid

(上記のコードはテストされていません)

于 2012-05-09T12:23:05.723 に答える