无法想到一个更具描述性的标题抱歉:(
所以无论如何 我遇到的问题是这个。 我有以下数组的事件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"
}
},
我无法理解为什么这不起作用。 如果有人能告诉我,我在这里做错了什么。 我非常感激。 此外,如果他们可以向我解释为什么我这样做错了或我应该改变什么使整个事情变得更快。 这也将非常感激。 现在需要不到一分钟的时间来提取这些数据。
答案 0 :(得分: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"
];
}
}