A curated collection of useful PHP snippets.
This package requires PHP 7.2 or higher.
You can install the package via composer:
composer require baka/support
The package will automatically register itself.
Returns true
if the provided function returns true
for all elements of an array, false
otherwise.
all([2, 3, 4, 5], function ($item) {
return $item > 1;
}); // true
Returns true
if the provided function returns true
for at least one element of an array, false
otherwise.
any([1, 2, 3, 4], function ($item) {
return $item < 2;
}); // true
Deep flattens an array.
deepFlatten([1, [2], [[3], 4], 5]); // [1, 2, 3, 4, 5]
Returns a new array with n
elements removed from the left.
drop([1, 2, 3]); // [2,3]
drop([1, 2, 3], 2); // [3]
Returns the last element for which the provided function returns a truthy value.
findLast([1, 2, 3, 4], function ($n) {
return ($n % 2) === 1;
});
// 3
Returns the index of the last element for which the provided function returns a truthy value.
findLastIndex([1, 2, 3, 4], function ($n) {
return ($n % 2) === 1;
});
// 2
Flattens an array up to the one level depth.
flatten([1, [2], 3, 4]); // [1, 2, 3, 4]
Groups the elements of an array based on the given function.
groupBy(['one', 'two', 'three'], 'strlen'); // [3 => ['one', 'two'], 5 => ['three']]
Checks a flat list for duplicate values. Returns true
if duplicate values exists and false
if values are all unique.
hasDuplicates([1, 2, 3, 4, 5, 5]); // true
Returns the head of a list.
head([1, 2, 3]); // 1
Returns the last element in an array.
last([1, 2, 3]); // 3
Retrieves all of the values for a given key:
pluck([
['product_id' => 'prod-100', 'name' => 'Desk'],
['product_id' => 'prod-200', 'name' => 'Chair'],
], 'name');
// ['Desk', 'Chair']
Mutates the original array to filter out the values specified.
$items = ['a', 'b', 'c', 'a', 'b', 'c'];
pull($items, 'a', 'c'); // $items will be ['b', 'b']
Filters the collection using the given callback.
reject(['Apple', 'Pear', 'Kiwi', 'Banana'], function ($item) {
return strlen($item) > 4;
}); // ['Pear', 'Kiwi']
Removes elements from an array for which the given function returns false.
remove([1, 2, 3, 4], function ($n) {
return ($n % 2) === 0;
});
// [0 => 1, 2 => 3]
Returns all elements in an array except for the first one.
tail([1, 2, 3]); // [2, 3]
Returns an array with n elements removed from the beginning.
take([1, 2, 3], 5); // [1, 2, 3]
take([1, 2, 3, 4, 5], 2); // [1, 2]
Filters out the elements of an array, that have one of the specified values.
without([2, 1, 2, 3], 1, 2); // [3]
Sorts a collection of arrays or objects by key.
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']]
Check if a string is ends with a given substring.
endsWith('Hi, this is me', 'me'); // true
Returns the first string there is between the strings from the parameter start and end.
```php
firstStringBetween('This is a [custom] string', '[', ']'); // custom
Compare two strings and returns true
if both strings are anagram, false
otherwise.
isAnagram('act', 'cat'); // true
Returns true
if the given string is lower case, false
otherwise.
isLowerCase('Morning shows the day!'); // false
isLowerCase('hello'); // true
Returns true
if the given string is upper case, false otherwise.
isUpperCase('MORNING SHOWS THE DAY!'); // true
isUpperCase('qUick Fox'); // false
Returns true
if the given string is a palindrome, false
otherwise.
palindrome('racecar'); // true
palindrome(2221222); // true
Check if a string starts with a given substring.
startsWith('Hi, this is me', 'Hi'); // true
Returns number of vowels in provided string.
Use a regular expression to count the number of vowels (A, E, I, O, U) in a string.
countVowels('sampleInput'); // 4
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.
decapitalize('FooBar'); // 'fooBar'
Check if a word / substring exist in a given string input.
Using strpos
to find the position of the first occurrence of a substring in a string. Returns either true
or false
contains('This is an example string', 'example'); // true
contains('This is an example string', 'hello'); // false
This project is licensed under the MIT License - see the License File for details
Thanks to appzcoder for the snippets!