Home

ArrayUtils

Awesome array manipulation utility for PHP

ArrayUtils is a library that provides a great way to manipulate arrays.

The evil PHP array functions give developers the pain of:

  • Some functions requires array first, but some functions require array last...

  • Some functions returns result, but some functions modify referenced variables...

  • Code was line-break because since it is a the function...

  • Each function has different parameters to the callback function...

  • No modern functions using arrays. Such as every, some

I created this library to solve these problems and make code flow like js-array

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.

Also, if you Using the arrow-function added in PHP 7.4, you can write more neatly.

$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
);

Last updated