1

想不出一个更具描述性的标题对不起:(

所以无论如何,我遇到的问题是这个。我有以下事件ID数组

$bosses = [
        "boss_behemoth" => [
            "title" => "The Shadow Behemoth",
            "chain" => [
                "pre1" => "CFBC4A8C-2917-478A-9063-1A8B43CC8C38",
                "pre2" => "36330140-7A61-4708-99EB-010B10420E39",
                "pre3" => "AFCF031A-F71D-4CEA-85E1-957179414B25",
                "pre4" => "E539A5E3-A33B-4D5F-AEED-197D2716F79B",
                "main" => "31CEBA08-E44D-472F-81B0-7143D73797F5"
            ]
        ],
        "boss_fireelemental " => [
            "title" => "The Fire Elemental",
            "chain" => [
                "pre1" => "2C833C11-5CD5-4D96-A4CE-A74C04C9A278",
                "pre2" => "33F76E9E-0BB6-46D0-A3A9-BE4CDFC4A3A4",
                "main" => "5E4E9CD9-DD7C-49DB-8392-C99E1EF4E7DF"
            ]
        ],
];

然后我使用下面的代码将它们放入一个新数组中。

foreach($bosses as &$eventchain)
{
    $root = $eventchain["chain"]["main"];
    $currentEvent = [
        $eventchain["chain"]["main"]  => [
            "title" => $eventchain["title"],
            "chain" => [],
            "state" => "Inactive"
        ]
    ];

    $linkcount = 0;

    foreach($eventchain["chain"] as &$chainlink)
    {
        $eventstatus = curl_get_contents("https://api.guildwars2.com/v1/events.json?event_id=".$chainlink."&world_id=".$world_id);
        $deets = curl_get_contents("https://api.guildwars2.com/v1/event_details.json?event_id=".$chainlink);

        $data = json_decode($deets);

        foreach($data->events as $event)
        {
            if($linkcount == count($eventchain["chain"]))
            {
                $currentEvent[$root]["chain"] = [
                    "main" => [
                        "title" =>  $event->name,
                        "level" =>  $event->level,
                        "map_id" =>  $event->map_id,
                        "type" =>  "Boss",
                        "location" => $event->location,
                        "state" => "Inactive"
                    ]
                ];
            }
            else
            {
                $currentEvent[$root]["chain"] = [
                    "pre".($linkcount + 1) => [
                        "title" =>  $event->name,
                        "level" =>  $event->level,
                        "map_id" => $event->map_id,
                        "type" =>  "Boss",
                        "location" => $event->location,
                        "state" => "Inactive"
                    ]
                ];
            }
        }

        $data = json_decode($eventstatus);
        $currentEvent[$root]["state"] = 'Inactive';

        foreach($data->events as $event)
        {
            if($linkcount == count($eventchain["chain"]))
            {
                $currentEvent[$root]["state"] =  $event->state;
                $currentEvent[$root]["chain"]["main"]["state"] =  $event->state;
            }
            else
            {
                if($currentEvent[$root]["state"] != "Active")
                {
                    if($event->state == "Active")
                    {
                        $currentEvent[$root]["state"] = "Pre-Event";
                        $currentEvent[$root]["chain"]["pre".($linkcount + 1)]["state"] =  $event->state;
                    }
                }
                else
                {
                    if($event->state == "Active")
                    {
                        $currentEvent[$root]["chain"]["pre".($linkcount + 1)]["state"] =  $event->state;
                    }
                }
            }
        }
        $events[count($events)] = $currentEvent;
        $linkcount++;
    }    
}

预期结果:

"31CEBA08-E44D-472F-81B0-7143D73797F5": {
    "title": "The Shadow Behemoth",
    "chain": {
        "pre1": {
            "title": "Drive back Underworld creatures by destroying portals in the Heartwoods.",
            "level": 10,
            "map_id": 15,
            "type": "Boss",
            "location": {
                "type": "sphere",
                "center": [-3749.1,
                -9158.17,
                -147.338],
                "radius": 2956.68,
                "rotation": 0
            },
            "state": "Inactive"
        },
        "pre2": {
            "title": "Drive back Underworld creatures by destroying portals in the swamp.",
            "level": 15,
            "map_id": 15,
            "type": "Boss",
            "location": {
                "type": "sphere",
                "center": [9978.66,
                -17928.1,
                32.8545],
                "radius": 2954.58,
                "rotation": 0
            },
            "state": "Inactive"
    },
    "state": "Inactive"
}

但它实际上导致:

[{
    "31CEBA08-E44D-472F-81B0-7143D73797F5": {
        "title": "The Shadow Behemoth",
        "chain": {
            "pre1": {
                "title": "Drive back Underworld creatures by destroying portals in the Heartwoods.",
                "level": 10,
                "map_id": 15,
                "type": "Boss",
                "location": {
                    "type": "sphere",
                    "center": [-3749.1,
                    -9158.17,
                    -147.338],
                    "radius": 2956.68,
                    "rotation": 0
                },
                "state": "Inactive"
            }
        },
        "state": "Inactive"
    }
},
{
    "31CEBA08-E44D-472F-81B0-7143D73797F5": {
        "title": "The Shadow Behemoth",
        "chain": {
            "pre2": {
                "title": "Drive back Underworld creatures by destroying portals in the swamp.",
                "level": 15,
                "map_id": 15,
                "type": "Boss",
                "location": {
                    "type": "sphere",
                    "center": [9978.66,
                    -17928.1,
                    32.8545],
                    "radius": 2954.58,
                    "rotation": 0
                },
                "state": "Inactive"
            }
        },
        "state": "Inactive"
    }
},

我不明白为什么这不起作用。如果有人能告诉我我在这里做错了什么。我将非常感激。另外,如果他们可以向我解释为什么我做错了,或者我应该改变什么以使整个事情进展得更快。这也将不胜感激。现在提取这些数据需要超过一分钟的时间。

4

1 回答 1

1

要获得所需的输出,您需要设置关联数组的值,而不是每次都覆盖整个数组,而且您需要移出$events[count($events)] = $currentEvent;顶级循环。这是正确设置数组的代码更改:

   foreach($data->events as $event)
   {
        if($linkcount == count($eventchain["chain"]))
        {
            $currentEvent[$root]["chain"]["main"] = [
                    "title" =>  $event->name,
                    "level" =>  $event->level,
                    "map_id" =>  $event->map_id,
                    "type" =>  "Boss",
                    "location" => $event->location,
                    "state" => "Inactive"
                ];
        }
        else
        {
            $currentEvent[$root]["chain"]["pre".($linkcount + 1)] = [
                    "title" =>  $event->name,
                    "level" =>  $event->level,
                    "map_id" => $event->map_id,
                    "type" =>  "Boss",
                    "location" => $event->location,
                    "state" => "Inactive"
                ];
        }
    }
于 2013-10-10T22:34:17.907 回答