1

Couldn't think of a more descriptive title sorry :(

So anyways, The issue i am having is this. I have below array of event ids

$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"
            ]
        ],
];

Then i use below code to put them into a new array.

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++;
    }    
}

The expected result:

"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"
}

But it actually results in:

[{
    "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"
    }
},

And i am failing to understand why this is not working. If anyone can tell me what the hell i am doing wrong here. I would very much appreciate it. Also if they can explain to me WHY i am doing this wrong or what i should change to make this entire thing go faster. That would also be much appreciated. it takes longer than a minute now to pull this data.

1 Answer 1

1

To get the desired output you'll need to set the values of the associative array, not overwrite the entire array each time, plus you'll want to move $events[count($events)] = $currentEvent; outside the top level loop. Here is the code change for setting the array correctly:

   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"
                ];
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.