30 seconds of PHP

Array

Click on a snippet's name to view its code.

all

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.

any

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.

deepFlatten

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.

drop

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.

findLast

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.

findLastIndex

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.

flatten

Flattens an array up to the one level depth.

Use array_push(), splat operator and array_values() to flatten the array.

groupBy

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.

hasDuplicates

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.

head

Returns the head of a list.

Use reset() to return the first item in the array.

last

Returns the last element in an array.

Use end() to return the last item in the array.

orderBy

Sorts a collection of arrays or objects by key.

Uses sort() on the provided array to sort the array based on $order and $attr.

pluck

Retrieves all of the values for a given key.

Use array_map() to map each object in the $items array to the provided $key.

pull

Mutates the original array to filter out the values specified.

Use array_values() and array_diff() to remove the specified values from $items.

reject

Filters the collection using the given callback.

Use array_values(), array_diff() and array_filter() to filter $items based on $func.

remove

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.

rotate

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.

tail

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.

take

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

Use array_slice() to remove $n items from the beginning of the array.

without

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.