Array Filter limit amount of results PHP -
i have following method
public function getnextavailablehousestoattack(\deadstreet\valueobject\house\collection $collection, $hordesize) { $houses = $collection->gethouses(); $housesthatcanbeattacked = array_filter($houses, function($house) use (&$hordesize) { if(!isset($house)) { return false; } $house = $this->housemodel->applymaxattackcapacity($house, $hordesize); if($this->housemodel->isattackable($house)) { return $house; } return false; }); return $housesthatcanbeattacked;
however, array can huge.
i want limit $housesthatcanbeattacked
whatever size of $hordesize
set to, need many houses there zombies in horde attack round.
however, array $housesthatcanbeattacked
end containing 1 million houses, there 100 in zombie horde.
is there way limit size of array built callback?
you use loop, , stop processing array when have enough houses.
$houses = $collection->gethouses(); housesthatcanbeattacked[]; $i = 0; foreach ($houses $house) { $house = $this->housemodel->applymaxattackcapacity($house, $hordesize); if ($this->housemodel->isattackable($house)) { housesthatcanbeattacked[] = $house; if (++$i == $hordesize) { break; } } }
Comments
Post a Comment