Php 如何在具有数组的数组中的foreach中嵌套元素
想不出更具描述性的标题抱歉:( 所以不管怎样, 我遇到的问题是这个。 我有以下事件ID数组Php 如何在具有数组的数组中的foreach中嵌套元素,php,arrays,Php,Arrays,想不出更具描述性的标题抱歉:( 所以不管怎样, 我遇到的问题是这个。 我有以下事件ID数组 $bosses = [ "boss_behemoth" => [ "title" => "The Shadow Behemoth", "chain" => [ "pre1" => "CFBC4A8C-2917-478A-9063-1A8B43CC8C38",
$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"
}
},
我不明白为什么这不起作用。
如果有人能告诉我我到底做错了什么。
我将非常感激。
还有,如果他们能向我解释为什么我做错了,或者我应该做些什么来让整个事情进行得更快。
这也将非常感谢。
现在提取此数据需要一分钟以上的时间。要获得所需的输出,您需要设置关联数组的值,而不是每次都覆盖整个数组,另外还需要移动
$events[计数($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"
];
}
}
要获得所需的输出,您需要设置关联数组的值,而不是每次都覆盖整个数组,另外还需要将
$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"
];
}
}