I’m currently trying to get into foreach loops and I’m trying to get some stuff displayed from an API – The only problem is we have 3 development servers and some of the time when we are running the script it seems quite sluggish to display the information as it iterates through them, Im not too sure if its the script or the server and so I was just wondering if there would be any way to cleanup or make this script a little bit faster:
foreach ($members as $member)
{
$level = $member['character']['level'];
if($level==90)
{
$mname = $member['character']['name'];
$character = $armory->getCharacter($mname);
$gear = $character->getGear();
$milevel = $gear['averageItemLevelEquipped'];
}
}
What we want it to do is get all the members names and then for each member to get the character information and then its gear also, I’d just like to check that, from what I’ve written, it’s only doing it once per member.
Thanks
(Turning my comments into an answer for the appropriate checking, editing, comments, etc…)
There’s nothing obviously wrong with the loop.
First step is to profile the code to find what bit is slow, then you can figure out what to do about it.
Here’s some speculation of may be causing it to be slow…
I presume this is talking to the WoW Armory which is going to involve network or database access for
getCharacterorgetGearwhich can seriously slow things down. That would be your most likely point of sluggishness. Consider caching their results, even if for a few minutes. This PHP WoW Armory project claims to do that for you, perhaps try that?If you can ask your API for a list of members who are level 90, rather than iterating through them all yourself, that might be more efficient depending on how your API does that search.
But profile first.