Awesome array manipulation utility for PHP
ArrayUtils is a library that provides a great way to manipulate arrays.
Processing arrays through this library makes the code flow much clearer than pure PHP.
Pure PHP's array functions, when nested, reverse the code and get deeper and deeper indentations.
ArrayUtils solves these problems and makes the code easier to figure out.
$playerFiles = scandir(Server::getInstance()->getDataPath() . "players/";
$onlinePlayers = Server::getInstance()->getOnlinePlayers();
$onlineNames = array_column(
array_map(
function(Player $player) : array{ return [strtolower($player->getName()), $player->getName()]; },
$onlinePlayers
), 1, 0
);
$playerNames = array_column(
array_map(
function(string $playerName) : array{ return [strtolower($playerName), $playerName]; },
array_map(
function(string $playerName) use($onlineNames) : string{ return $onlineNames[strtolower($playerName)] ?? $playerName; },
array_map(
function(string $fileName) : string{ return substr($fileName, 0, -strlen(\".dat\")); },
array_filter($playerFiles, function(string $fileName) : bool{ return substr($fileName, -strlen(\".dat\")) === \".dat\"; })
)
)
), 1, 0
);
use kim\present\utils\arrays\ArrayUtils;
$playerFiles = scandir(Server::getInstance()->getDataPath() . "players/");
$onlinePlayers = Server::getInstance()->getOnlinePlayers();
$onlineNames = ArrayUtils::mapAssocFromAs($onlinePlayers, function(Player $player) : array{ return [strtolower($player->getName()), $player->getName()]; });
$playerNames = ArrayUtils::filterFrom($playerFiles, function(string $fileName) : bool{ return substr($fileName, -strlen(".dat")) === ".dat"; })
->map(function(string $fileName) : string{ return substr($fileName, 0, -strlen(".dat")); })
->map(function(string $playerName) use($onlineNames) : string{ return $onlineNames[strtolower($playerName)] ?? $playerName; })
->mapAssocAs(function(string $playerName) : array{ return [strtolower($playerName), $playerName]; });
use kim\present\utils\arrays\ArrayUtils;
$playerFiles = scandir(Server::getInstance()->getDataPath() . "players/");
$onlinePlayers = Server::getInstance()->getOnlinePlayers();
$onlineNames = ArrayUtils::mapAssocFromAs($onlinePlayers, fn(Player $player) => [strtolower($player->getName()), $player->getName()]);
$playerNames = ArrayUtils::filterFrom($playerFiles, fn(string $fileName) => substr($fileName, -strlen(".dat")) === ".dat")
->map(fn(string $fileName) => substr($fileName, 0, -strlen(".dat")))
->map(fn(string $playerName) => $onlineNames[strtolower($playerName)] ?? $playerName)
->mapAssocAs(fn(string $playerName) => [strtolower($playerName), $playerName]);