npm npm npm npm

all


  • title: all
  • tags: array,beginner

Returns true if the provided function returns true for all elements of an array, false otherwise.

  • Use array_filter() and count() to check if $func returns true for all the elements in $items.
function all($items, $func)
{
  return count(array_filter($items, $func)) === count($items);
}
all([2, 3, 4, 5], function ($item) {
  return $item > 1;
}); // true

any


  • title: any
  • tags: array,beginner

Returns true if the provided function returns true for at least one element of an array, false otherwise.

  • Use array_filter() and count() to check if $func returns true for any of the elements in $items.
function any($items, $func)
{
  return count(array_filter($items, $func)) > 0;
}
any([1, 2, 3, 4], function ($item) {
  return $item < 2;
}); // true

approximatelyEqual


  • title: approximatelyEqual
  • tags: math,beginner

Checks if two numbers are approximately equal to each other.

  • Use abs() to compare the absolute difference of the two values to $epsilon.
  • Omit the third parameter, $epsilon, to use a default value of 0.001.
function approximatelyEqual($number1, $number2, $epsilon = 0.001)
{
  return abs($number1 - $number2) < $epsilon;
}
approximatelyEqual(10.0, 10.00001); // true

approximatelyEqual(10.0, 10.01); // false

average


  • title: average
  • tags: math,beginner

Returns the average of two or more numbers.

  • Use array_sum() for all the values in $items and return the result divided by their count().
function average(...$items)
{
  $count = count($items);
  
  return $count === 0 ? 0 : array_sum($items) / $count;
}
average(1, 2, 3); // 2

clampNumber


  • title: clampNumber
  • tags: math,beginner

Clamps $num within the inclusive range specified by the boundary values $a and $b.

  • If $num falls within the range, return $num.
  • Otherwise, return the nearest number in the range, using min() and max().
function clampNumber($num, $a, $b)
{
  return max(min($num, max($a, $b)), min($a, $b));
}
clampNumber(2, 3, 5); // 3
clampNumber(1, -1, -5); // -1

compose


  • title: compose
  • tags: function,intermediate

Return a new function that composes multiple functions into a single callable.

  • Use array_reduce() to perform right-to-left function composition.
function compose(...$functions)
{
  return array_reduce(
    $functions,
    function ($carry, $function) {
      return function ($x) use ($carry, $function) {
        return $function($carry($x));
      };
    },
    function ($x) {
      return $x;
    }
  );
}
$compose = compose(
  // add 2
  function ($x) {
    return $x + 2;
  },
  // multiply 4
  function ($x) {
    return $x * 4;
  }
);
$compose(3); // 20

countVowels


  • title: countVowels
  • tags: string,regexp,beginner

Returns number of vowels in the provided string.

  • Use a regular expression to count the number of vowels (a, e, i, o and ua) in a string.
function countVowels($string)
{
  preg_match_all('/[aeiou]/i', $string, $matches);

  return count($matches[0]);
}
countVowels('sampleInput'); // 4

curry


  • title: curry
  • tags: function,advanced

Curries a function to take arguments in multiple calls.

  • If the number of provided arguments ($args) is sufficient, call the passed function, $function.
  • Otherwise, return a curried function that expects the rest of the arguments.
function curry($function)
{
  $accumulator = function ($arguments) use ($function, &$accumulator) {
    return function (...$args) use ($function, $arguments, $accumulator) {
      $arguments = array_merge($arguments, $args);
      $reflection = new ReflectionFunction($function);
      $totalArguments = $reflection->getNumberOfRequiredParameters();

      if ($totalArguments <= count($arguments)) {
        return $function(...$arguments);
      }

      return $accumulator($arguments);
    };
  };

  return $accumulator([]);
}
$curriedAdd = curry(
  function ($a, $b) {
    return $a + $b;
  }
);

$add10 = $curriedAdd(10);
var_dump($add10(15)); // 25

decapitalize


  • title: decapitalize
  • tags: string,beginner

Decapitalizes the first letter of a string.

  • Decapitalizes the first letter of the string and then adds it with rest of the string.
  • Omit the $upperRest parameter to keep the rest of the string intact, or set it to true to convert to uppercase.
function decapitalize($string, $upperRest = false)
{
  return lcfirst($upperRest ? strtoupper($string) : $string);
}
decapitalize('FooBar'); // 'fooBar'

deepFlatten


  • title: deepFlatten
  • tags: array,recursion,intermediate

Deep flattens an array.

  • Use recursion.
  • Use array_push, splat operator and an empty array to flatten the array.
  • Recursively flatten each element that is an array.
function deepFlatten($items)
{
  $result = [];
  foreach ($items as $item) {
    if (!is_array($item)) {
      $result[] = $item;
    } else {
      array_push($result, ...deepFlatten($item));
    }
  }

  return $result;
}
deepFlatten([1, [2], [[3], 4], 5]); // [1, 2, 3, 4, 5]

drop


  • title: drop
  • tags: array,beginner

Returns a new array with $n elements removed from the left.

  • Use array_slice() to remove $n elements from the left.
  • Omit the second argument, $n, to only remove one element.
function drop($items, $n = 1)
{
  return array_slice($items, $n);
}
drop([1, 2, 3]); // [2,3]
drop([1, 2, 3], 2); // [3]

endsWith


  • title: endsWith
  • tags: string,beginner

Checks if a string is ends with a given substring.

  • Use strrpos() in combination with strlen to find the position of $needle in $haystack.
function endsWith($haystack, $needle)
{
  return strrpos($haystack, $needle) === (strlen($haystack) - strlen($needle));
}
endsWith('Hi, this is me', 'me'); // true

factorial


  • title: factorial
  • tags: math,recursion,beginner

Calculates the factorial of a number.

  • Use recursion.
  • If $n is less then or equal to 1, return 1.
  • Otherwise, return the product of $n and the factorial of $n -1.
function factorial($n)
{
  if ($n <= 1) {
    return 1;
  }

  return $n * factorial($n - 1);
}
factorial(6); // 720

fibonacci


  • title: fibonacci
  • tags: math,intermediate

Generates an array, containing the Fibonacci sequence, up until the nth term.

  • Create an empty array, initializing the first two values (0 and 1).
  • Loop from 2 through $n and add values into the array, using the sum of the last two values.
function fibonacci($n)
{
  $sequence = [0, 1];

  for ($i = 2; $i < $n; $i++) {
    $sequence[$i] = $sequence[$i-1] + $sequence[$i-2];
  }

  return $sequence;
}
fibonacci(6); // [0, 1, 1, 2, 3, 5]

findLast


  • title: findLast
  • tags: array,beginner

Returns the last element for which the provided function returns a truthy value.

  • Use array_filter() to remove elements for which $func returns falsy values, array_pop() to get the last one.
function findLast($items, $func)
{
  $filteredItems = array_filter($items, $func);

  return array_pop($filteredItems);
}
findLast([1, 2, 3, 4], function ($n) {
  return ($n % 2) === 1;
});
// 3

findLastIndex


  • title: findLastIndex
  • tags: array,beginner

Returns the index of the last element for which the provided function returns a truthy value.

  • Use array_keys() and array_filter() to remove elements for which $func returns falsy values, array_pop() to get the last one.
function findLastIndex($items, $func)
{
  $keys = array_keys(array_filter($items, $func));

  return array_pop($keys);
}
findLastIndex([1, 2, 3, 4], function ($n) {
  return ($n % 2) === 1;
});
// 2

firstStringBetween


  • title: firstStringBetween
  • tags: string,beginner

Returns the first string there is between the strings from the parameter $start and $end.

  • Use trim() and strstr() to find the string contained between $start and $end.
function firstStringBetween($haystack, $start, $end)
{
  return trim(strstr(strstr($haystack, $start), $end, true), $start . $end);
}
firstStringBetween('This is a [custom] string', '[', ']'); // custom

flatten


  • title: flatten
  • tags: array,intermediate

Flattens an array up to the one level depth.

  • Use array_push(), splat operator and array_values() to flatten the array.
function flatten($items)
{
  $result = [];
  foreach ($items as $item) {
    if (!is_array($item)) {
      $result[] = $item;
    } else {
      array_push($result, ...array_values($item));
    }
  }

  return $result;
}
flatten([1, [2], 3, 4]); // [1, 2, 3, 4]

gcd


  • title: gcd
  • tags: math,recursion,intermediate

Calculates the greatest common divisor between two or more numbers.

  • Use recursion.
  • Use array_reduce() with the gcd function to appy to all elements in the $numbers list.
  • Base case is when y equals 0. In this case, return x.
  • Otherwise, return the gcd of y and the remainder of the division x/y.
function gcd(...$numbers)
{
  if (count($numbers) > 2) {
    return array_reduce($numbers, 'gcd');
  }

  $r = $numbers[0] % $numbers[1];
  return $r === 0 ? abs($numbers[1]) : gcd($numbers[1], $r);
}
gcd(8, 36); // 4
gcd(12, 8, 32); // 4

groupBy


  • title: groupBy
  • tags: array,intermediate

Groups the elements of an array based on the given function.

  • Use call_use_func() with $func on $items to group them based on $func.
function groupBy($items, $func)
{
  $group = [];
  foreach ($items as $item) {
    if ((!is_string($func) && is_callable($func)) || function_exists($func)) {
      $key = call_user_func($func, $item);
      $group[$key][] = $item;
    } elseif (is_object($item)) {
      $group[$item->{$func}][] = $item;
    } elseif (isset($item[$func])) {
      $group[$item[$func]][] = $item;
    }
  }

  return $group;
}
groupBy(['one', 'two', 'three'], 'strlen'); // [3 => ['one', 'two'], 5 => ['three']]

hasDuplicates


  • title: hasDuplicates
  • tags: array,beginner

Checks a flat list for duplicate values, returning true if duplicate values exists and false if values are all unique.

  • Use count() and array_unique() to check $items for duplicate values.
function hasDuplicates($items)
{
  return count($items) > count(array_unique($items));
}
hasDuplicates([1, 2, 3, 4, 5, 5]); // true

head


  • title: head
  • tags: array,beginner

Returns the head of a list.

  • Use reset() to return the first item in the array.
function head($items)
{
  return reset($items);
}
head([1, 2, 3]); // 1

isAnagram


  • title: isAnagram
  • tags: string,beginner

Compare two strings and returns true if both strings are anagram, false otherwise.

  • Use count_chars() to compare $string1 and $string2.
function isAnagram($string1, $string2)
{
  return count_chars($string1, 1) === count_chars($string2, 1);
}
isAnagram('act', 'cat'); // true

isContains


  • title: isContains
  • tags: string,beginner

Check if a word / substring exists in a given string input.

  • Using strpos() to find the position of the first occurrence of a substring in a string.
function isContains($string, $needle)
{
  return strpos($string, $needle) === false ? false : true;
}
isContains('This is an example string', 'example'); // true
isContains('This is an example string', 'hello'); // false

isEven


  • title: isEven
  • tags: math,beginner

Returns true if the given number is even, false otherwise.

  • Checks whether a number is odd or even using the modulo (%) operator.
  • Returns true if the number is even, false if the number is odd.
function isEven($number)
{
  return ($number % 2) === 0;
}
isEven(4); // true

isLowerCase


  • title: isLowerCase
  • tags: string,beginner

Returns true if the given string is lower case, false otherwise.

  • Convert the given string to lower case, using strtolower and compare it to the original.
function isLowerCase($string)
{
  return $string === strtolower($string);
}
isLowerCase('Morning shows the day!'); // false
isLowerCase('hello'); // true

isPrime


  • title: isPrime
  • tags: math,beginner

Checks if the provided integer is a prime number.

  • Check numbers from 2 to the square root of the given number.
  • Return false if any of them divides the given number, else return true, unless the number is less than 2.
function isPrime($number)
{
  $boundary = floor(sqrt($number));
  for ($i = 2; $i <= $boundary; $i++) {
    if ($number % $i === 0) {
      return false;
    }
  }

  return $number >= 2;
}
isPrime(3); // true

isUpperCase


  • title: isUpperCase
  • tags: string,beginner

Returns true if the given string is upper case, false otherwise.

  • Convert the given string to upper case, using strtoupper and compare it to the original.
function isUpperCase($string)
{
  return $string === strtoupper($string);
}
isUpperCase('MORNING SHOWS THE DAY!'); // true
isUpperCase('qUick Fox'); // false

last


  • title: last
  • tags: array,beginner

Returns the last element in an array.

  • Use end() to return the last item in the array.
function last($items)
{
  return end($items);
}
last([1, 2, 3]); // 3

lcm


  • title: lcm
  • tags: math,intermediate

Returns the least common multiple of two or more numbers.

  • Use the greatest common divisor (GCD) formula and the fact that lcm(x,y) = x * y / gcd(x,y) to determine the least common multiple.
  • The GCD formula uses recursion.
function lcm(...$numbers)
{
  $ans = $numbers[0];
  for ($i = 1, $max = count($numbers); $i < $max; $i++) {
    $ans = (($numbers[$i] * $ans) / gcd($numbers[$i], $ans));
  }

  return $ans;
}
lcm(12, 7); // 84
lcm(1, 3, 4, 5); // 60

maxN


  • title: maxN
  • tags: math,array,intermediate

Returns the maximum value from the provided array.

  • Use array_filter() and max() to find the maximum value in an array.
function maxN($numbers)
{
  $maxValue = max($numbers);
  $maxValueArray = array_filter($numbers, function ($value) use ($maxValue) {
    return $maxValue === $value;
  });

  return count($maxValueArray);
}
maxN([1, 2, 3, 4, 5, 5]); // 2
maxN([1, 2, 3, 4, 5]); // 1

median


  • title: median
  • tags: math,array,beginner

Returns the median of an array of numbers.

  • Find the middle of the array, use sort() to sort the values.
  • Return the number at the midpoint if the array's length is odd, otherwise the average of the two middle numbers.
function median($numbers)
{
  sort($numbers);
  $totalNumbers = count($numbers);
  $mid = floor($totalNumbers / 2);

  return ($totalNumbers % 2) === 0 ? ($numbers[$mid - 1] + $numbers[$mid]) / 2 : $numbers[$mid];
}
median([1, 3, 3, 6, 7, 8, 9]); // 6
median([1, 2, 3, 6, 7, 9]); // 4.5

memoize


  • title: memoize
  • tags: function,advanced

Returns the memoized (cached) function.

  • Create an empty cache by instantiating a new array.
  • Return a function which takes a single argument to be supplied to the memoized function by first checking if the function's output for that specific input value is already cached, or store and return it if not.
  • Allow access to the cache by setting it as a property on the returned function.
function memoize($func)
{
  return function () use ($func) {
    static $cache = [];

    $args = func_get_args();
    $key = serialize($args);
    $cached = true;

    if (!isset($cache[$key])) {
      $cache[$key] = $func(...$args);
      $cached = false;
    }

    return ['result' => $cache[$key], 'cached' => $cached];
  };
}
$memoizedAdd = memoize(
  function ($num) {
    return $num + 10;
  }
);

var_dump($memoizedAdd(5)); // ['result' => 15, 'cached' => false]
var_dump($memoizedAdd(6)); // ['result' => 16, 'cached' => false]
var_dump($memoizedAdd(5)); // ['result' => 15, 'cached' => true]

minN


  • title: minN
  • tags: math,array,intermediate

Returns the minimum value from the provided array.

  • Use array_filter() and min() to find the minimum value in an array.
function minN($numbers)
{
  $minValue = min($numbers);
  $minValueArray = array_filter($numbers, function ($value) use ($minValue) {
    return $minValue === $value;
  });

  return count($minValueArray);
}
minN([1, 1, 2, 3, 4, 5, 5]); // 2
minN([1, 2, 3, 4, 5]); // 1

once


  • title: once
  • tags: function,intermediate

Call a function only once.

  • Return a function, which only calls the provided function, $function, if $called is false and sets $called to true.
function once($function)
{
  return function (...$args) use ($function) {
    static $called = false;
    if ($called) {
      return;
    }
    $called = true;
    return $function(...$args);
  };
}
$add = function ($a, $b) {
  return $a + $b;
};

$once = once($add);

var_dump($once(10, 5)); // 15
var_dump($once(20, 10)); // null

orderBy


  • title: orderBy
  • tags: array,advanced

Sorts a collection of arrays or objects by key.

  • Uses sort() on the provided array to sort the array based on $order and $attr.
function orderBy($items, $attr, $order)
{
  $sortedItems = [];
  foreach ($items as $item) {
    $key = is_object($item) ? $item->{$attr} : $item[$attr];
    $sortedItems[$key] = $item;
  }
  if ($order === 'desc') {
    krsort($sortedItems);
  } else {
    ksort($sortedItems);
  }

  return array_values($sortedItems);
}
orderBy(
  [
    ['id' => 2, 'name' => 'Joy'],
    ['id' => 3, 'name' => 'Khaja'],
    ['id' => 1, 'name' => 'Raja']
  ],
  'id',
  'desc'
); // [['id' => 3, 'name' => 'Khaja'], ['id' => 2, 'name' => 'Joy'], ['id' => 1, 'name' => 'Raja']]

palindrome


  • title: palindrome
  • tags: string,beginner

Returns true if the given string is a palindrome, false otherwise.

  • Check if the value of strrev($string) is equal to the passed $string.
function palindrome($string)
{
  return strrev($string) === (string) $string;
}
palindrome('racecar'); // true
palindrome(2221222); // true

pluck


  • title: pluck
  • tags: array,beginner

Retrieves all of the values for a given key.

  • Use array_map() to map each object in the $items array to the provided $key.
function pluck($items, $key)
{
  return array_map( function($item) use ($key) {
    return is_object($item) ? $item->$key : $item[$key];
  }, $items);
}
pluck([
  ['product_id' => 'prod-100', 'name' => 'Desk'],
  ['product_id' => 'prod-200', 'name' => 'Chair'],
], 'name');
// ['Desk', 'Chair']

pull


  • title: pull
  • tags: array,beginner

Mutates the original array to filter out the values specified.

  • Use array_values() and array_diff() to remove the specified values from $items.
function pull(&$items, ...$params)
{
  $items = array_values(array_diff($items, $params));
  return $items;
}
$items = ['a', 'b', 'c', 'a', 'b', 'c'];
pull($items, 'a', 'c'); // $items will be ['b', 'b']

reject


  • title: reject
  • tags: array,beginner

Filters the collection using the given callback.

  • Use array_values(), array_diff() and array_filter() to filter $items based on $func.
function reject($items, $func)
{
  return array_values(array_diff($items, array_filter($items, $func)));
}
reject(['Apple', 'Pear', 'Kiwi', 'Banana'], function ($item) {
  return strlen($item) > 4;
}); // ['Pear', 'Kiwi']

remove


  • title: remove
  • tags: array,beginner

Removes elements from an array for which the given function returns false.

  • Use array_filter() to find array elements that return truthy values and array_diff_keys() to remove the elements not contained in $filtered.
function remove($items, $func)
{
  $filtered = array_filter($items, $func);

  return array_diff_key($items, $filtered);
}
remove([1, 2, 3, 4], function ($n) {
  return ($n % 2) === 0;
});
// [0 => 1, 2 => 3]

rotate


  • title: rotate
  • tags: array,beginner

Rotates the array (in left direction) by the number of shifts.

  • Given the $shift index, merge the array values after $shift with the values before $shift.
function rotate($array, $shift = 1)
{
  for ($i = 0; $i < $shift; $i++) {
    array_push($array, array_shift($array));
  }

  return $array;
}
rotate([1, 3, 5, 2, 4]); // [3, 5, 2, 4, 1]
rotate([1, 3, 5, 2, 4], 2); // [5, 2, 4, 1, 3]

shorten


  • title: shorten
  • tags: string,beginner

Returns a shortened string.

  • Use mb_strlen(), mb_substr() and rtrim() to shorten a string to a give number of characters.
function shorten($input, $length = 100, $end = '...')
{
  if (mb_strlen($input) <= $length) {
    return $input;
  }

  return rtrim(mb_substr($input, 0, $length, 'UTF-8')) . $end;
}
shorten('The quick brown fox jumped over the lazy dog', 15); // The quick brown...

slugify


  • title: slugify
  • tags: string,intermediate

Converts a string to a URL-friendly slug.

  • Uses preg_replace() to replace invalid chars with dashes, iconv() to convert the text to ASCII, strtolower() and trim() to convert to lowercase and remove extra whitespace.
function slugify($text) {
  $text = preg_replace('~[^\pL\d]+~u', '-', $text);
  $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
  $text = preg_replace('~[^-\w]+~', '', $text);
  $text = preg_replace('~-+~', '-', $text);
  $text = strtolower($text);
  $text = trim($text, " \t\n\r\0\x0B-");
  if (empty($text)) {
    return 'n-a';
  }
  return $text;
}
slugify('Hello World'); // 'hello-world'

startsWith


  • title: startsWith
  • tags: string,beginner

Check if a string starts with a given substring.

  • Use strpos() to find the position of $needle in $haystack.
function startsWith($haystack, $needle)
{
  return strpos($haystack, $needle) === 0;
}
startsWith('Hi, this is me', 'Hi'); // true

tail


  • title: tail
  • tags: array,beginner

Returns all elements in an array except for the first one.

  • Use array_slice() and count() to return all the items in the array except for the first one.
function tail($items)
{
  return count($items) > 1 ? array_slice($items, 1) : $items;
}
tail([1, 2, 3]); // [2, 3]

take


  • title: take
  • tags: array,beginner

Returns an array with $n elements removed from the beginning.

  • Use array_slice() to remove $n items from the beginning of the array.
function take($items, $n = 1)
{
  return array_slice($items, 0, $n);
}
take([1, 2, 3], 5); // [1, 2, 3]
take([1, 2, 3, 4, 5], 2); // [1, 2]

without


  • title: without
  • tags: array,beginner

Filters out the elements of an array, that have one of the specified values.

  • Use array_values() and array_diff() to remove any values in $params from $items.
function without($items, ...$params)
{
  return array_values(array_diff($items, $params));
}
without([2, 1, 2, 3], 1, 2); // [3]