PHP code example of melvdouc / array-utils

1. Go to this page and download the library: Download melvdouc/array-utils library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

melvdouc / array-utils example snippets


$lotteryNumbers = [15, 4, 16, 42, 23, 8];
ArrayUtils::bubbleSort($lotteryNumbers, function (int $a, int $b) {
  return $a - $b;
}); // [ 4, 8, 15, 16, 23, 42 ]

$numbers = [1, 2, 3];

function isEven(int $n)
{
  return $n % 2 === 0;
}

ArrayUtils::every($numbers, "isEven"); // false
ArrayUtils::some($numbers, "isEven"); // true

$firstEvenNumber = ArrayUtils::find($numbers, "isEven"); // 2

ArrayUtils::flatten([1, [2, [3], 4]]);
// ↪ [ 1, 2, [ 3 ], 4 ]
ArrayUtils::flatten([1, [2, [3], 4]], INF);
// ↪ [ 1, 2, 3, 4 ]

$squareNumbers = ArrayUtils::from(3, fn ($i) => ($i + 1) ** 2);
// ↪ [ 1, 4, 9 ]

$numbersByParity = ArrayUtils::groupBy($numbers, function (int $n) {
  return ($n % 2 === 0) ? "even" : "odd";
});
// ↪ [ "odd" => [ 1, 3 ], "even" => [ 2 ] ]